共用方式為


Process.HasExited 屬性

定義

取得值,指出相關的處理序是否已經終止。

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

屬性值

如果 Process 元件所參考的作業系統處理序已終止,則為 true,否則為 false

屬性

例外狀況

沒有與這個物件關聯的處理序。

無法擷取處理序的結束代碼。

您正在嘗試存取於遠端電腦上執行之處理序的 HasExited 屬性。 這個屬性僅供在本機電腦執行的處理序使用。

範例

下列範例會啟動記事本的實例。 然後,它會擷取相關聯進程的物理記憶體使用量,間隔上限為10秒。 此範例會偵測進程是否在經過10秒之前結束。 此範例會在 10 秒後仍在執行時關閉進程。

#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

備註

的值trueHasExited表示相關聯的進程已正常或異常終止。 您可以呼叫 CloseMainWindowKill,要求或強制相關聯的進程結束。 如果進程開啟句柄,當進程結束時,操作系統會釋放進程記憶體,但會保留進程的系統管理資訊,例如句柄、結束代碼和結束時間。 若要取得這項資訊,您可以使用 ExitCodeExitTime 屬性。 這些屬性會自動填入此元件啟動的進程。 系統管理資訊會在與系統進程相關聯的所有 Process 元件遭到終結時釋放,而且不會再保留已結束進程的句柄。

進程可以獨立於程式代碼中終止。 如果您使用這個元件啟動進程,系統就會自動更新 的值 HasExited ,即使相關聯的進程獨立結束也一樣。

注意

當標準輸出重新導向至異步事件處理程式時,當這個屬性傳 true回 時,輸出處理可能不會完成。 若要確保異步事件處理已完成,請在檢查 HasExited之前呼叫WaitForExit()不採用參數的多載。

適用於

另請參閱