Process.HasExited 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出關聯的進程是否已終止。
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
屬性值
true 如果元件所參考 Process 的作業系統程序已經終止;否則, false。
- 屬性
例外狀況
物件本身沒有相關聯的程序。
無法取得該程序的退出代碼。
你正在嘗試存取遠端電腦上執行的程序的 HasExited 屬性。 此特性僅適用於在本地電腦上執行的程序。
範例
以下範例啟動一個 Notepad 實例。 接著它會以 2 秒的間隔擷取相關程序的物理記憶體使用量,最多可持續 10 秒。 範例中判斷程序是否在 10 秒內退出。 範例中若程序在 10 秒後仍在運行,則會關閉程序。
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);
}
}
}
}
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading
try
use myProcess = Process.Start "Notepad.exe"
// Display physical memory usage 5 times at intervals of 2 seconds.
let mutable i = 0
while i < 5 && not myProcess.HasExited do
// Discard cached information about the process.
myProcess.Refresh()
// Print working set to console.
printfn $"Physical Memory Usage: {myProcess.WorkingSet64}"
// Wait 2 seconds.
Thread.Sleep 2000
i <- i + 1
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow() |> ignore
// Free resources associated with process.
myProcess.Close()
with
| :? Win32Exception
| :? FileNotFoundException as e ->
printfn "The following exception was raised: "
printfn $"{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 表示相關程序已正常或異常終止。 你可以透過呼叫 CloseMainWindow 或 Kill強制相關程序退出。 如果某個 handle 對程序開放,作業系統會在程序退出後釋放程序記憶體,但保留有關程序的管理資訊,例如句柄、退出代碼和退出時間。 要取得這些資訊,你可以使用和ExitCode屬性。ExitTime 這些屬性會自動為由此元件啟動的程序填充。 當 Process 所有與系統程序相關的元件被銷毀,且不再持有退出程序的句柄時,管理資訊才會被釋放。
程序可以獨立於你的程式碼終止。 如果你是用這個元件啟動程序,系統會自動更新 的 HasExited 值,即使相關程序獨立退出。
Note
當標準輸出被導向到非同步事件處理程序時,當此特性返回 true時,輸出處理可能尚未完成。 為確保非同步事件處理已完成,請在檢查WaitForExit()前呼叫不取參數的超載。HasExited