Process.HasExited 屬性

定義

取得值,指出相關的處理序是否已經終止。Gets a value indicating whether the associated process has been terminated.

public:
 property bool HasExited { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool HasExited { get; }
member this.HasExited : bool
Public ReadOnly Property HasExited As Boolean

屬性值

如果 Process 元件所參考的作業系統處理序已終止,則為 true,否則為 falsetrue if the operating system process referenced by the Process component has terminated; otherwise, false.

屬性

例外狀況

沒有與這個物件關聯的處理序。There is no process associated with the object.

無法擷取處理序的結束代碼。The exit code for the process could not be retrieved.

您正在嘗試存取於遠端電腦上執行之處理序的 HasExited 屬性。You are trying to access the HasExited property for a process that is running on a remote computer. 這個屬性僅供在本機電腦執行的處理序使用。This property is available only for processes that are running on the local computer.

範例

下列範例會啟動記事本的實例。The following example starts an instance of Notepad. 然後,它會以2秒的間隔來抓取相關進程的實體記憶體使用量,最多10秒。It then retrieves the physical memory usage of the associated process at 2 second intervals for a maximum of 10 seconds. 此範例會偵測進程是否會在10秒之前結束。The example detects whether the process exits before 10 seconds have elapsed. 此範例會關閉進程(如果它仍然在10秒後執行)。The example closes the process if it is still running after 10 seconds.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   try
   {
      Process^ myProcess;
      myProcess = Process::Start(  "Notepad.exe" );
      
      // Display physical memory usage 5 times at intervals of 2 seconds.
      for ( int i = 0; i < 5; i++ )
      {
         if (  !myProcess->HasExited )
         {
            
            // Discard cached information about the process.
            myProcess->Refresh();
            
            // Print working set to console.
            Console::WriteLine( "Physical Memory Usage : {0}", myProcess->WorkingSet.ToString() );
            
            // Wait 2 seconds.
            Thread::Sleep( 2000 );
         }
         else
         {
            break;
         }

      }
      myProcess->CloseMainWindow();
      
      // Free resources associated with process.
      myProcess->Close();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The following exception was raised: " );
      Console::WriteLine( e->Message );
   }

}

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace ProcessSample
{
    class MyProcessClass
    {
        public static void Main()
        {
            try
            {
                using (Process myProcess = Process.Start("Notepad.exe"))
                {
                    // Display physical memory usage 5 times at intervals of 2 seconds.
                    for (int i = 0; i < 5; i++)
                    {
                        if (!myProcess.HasExited)
                        {
                            // Discard cached information about the process.
                            myProcess.Refresh();
                            // Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}");
                            // Wait 2 seconds.
                            Thread.Sleep(2000);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow();
                    // Free resources associated with process.
                    myProcess.Close();
                }
            }
            catch (Exception e) when (e is Win32Exception || e is FileNotFoundException)
            {
                Console.WriteLine("The following exception was raised: ");
                Console.WriteLine(e.Message);
            }
        }
    }
}
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.IO
Imports System.Threading

Namespace Process_Sample
    Class MyProcessClass

        Public Shared Sub Main()
            Try
                Using myProcess = Process.Start("Notepad.exe")
                    ' Display physical memory usage 5 times at intervals of 2 seconds.
                    Dim i As Integer
                    For i = 0 To 4
                        If Not myProcess.HasExited Then

                            ' Discard cached information about the process.
                            myProcess.Refresh()
                            ' Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}")
                            ' Wait 2 seconds.
                            Thread.Sleep(2000)
                        Else
                            Exit For
                        End If

                    Next i

                    ' Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow()
                    ' Free resources associated with process.
                    myProcess.Close()
                End Using
            Catch e As Exception When TypeOf e Is Win32Exception Or TypeOf e Is FileNotFoundException
                Console.WriteLine("The following exception was raised: ")
                Console.WriteLine(e.Message)
            End Try
        End Sub
    End Class
End Namespace 'Process_Sample

備註

HasExitedtrue 值表示相關聯的進程已結束,通常為或異常。A value of true for HasExited indicates that the associated process has terminated, either normally or abnormally. 您可以藉由呼叫 CloseMainWindowKill,來要求或強制關聯的進程結束。You can request or force the associated process to exit by calling CloseMainWindow or Kill. 如果控制碼已開啟處理常式,作業系統會在進程結束時釋放進程記憶體,但會保留進程的系統管理資訊,例如控制碼、結束代碼和結束時間。If a handle is open to the process, the operating system releases the process memory when the process has exited, but retains administrative information about the process, such as the handle, exit code, and exit time. 若要取得此資訊,您可以使用 ExitCodeExitTime 屬性。To get this information, you can use the ExitCode and ExitTime properties. 系統會針對此元件所啟動的進程,自動填入這些屬性。These properties are populated automatically for processes that were started by this component. 當與系統進程相關聯的所有 Process 元件都已終結,並不會再對結束的進程保留任何控制碼時,就會釋放管理資訊。The administrative information is released when all the Process components that are associated with the system process are destroyed and hold no more handles to the exited process.

進程可以獨立于您的程式碼之外終止。A process can terminate independently of your code. 如果您使用此元件啟動進程,系統會自動更新 HasExited 的值,即使關聯的進程獨立結束也一樣。If you started the process using this component, the system updates the value of HasExited automatically, even if the associated process exits independently.

注意

當標準輸出已重新導向至非同步事件處理常式時,當此屬性傳回 true時,輸出處理可能不會完成。When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this property returns true. 為確保非同步事件處理已經完成,請先呼叫不採用參數的 WaitForExit() 多載,再檢查 HasExitedTo ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter before checking HasExited.

安全性

LinkDemand
針對立即呼叫者的完全信任的。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

適用於

另請參閱