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

以下示例启动记事本的实例。 然后,它以 2 秒的间隔检索关联进程的物理内存使用率,最长为 10 秒。 该示例检测进程是否在 10 秒之前退出。 如果进程在 10 秒后仍在运行,则此示例将关闭该过程。

#using <System.dll> using namespace System; using namespace System::Diagnostics; using namespace System::Threading; int main() 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 ); 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() 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); 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() 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) 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

true HasExited 个值,指示关联进程已正常终止或异常。 可以通过调用 CloseMainWindow Kill 或强制关联进程退出。 如果句柄对进程打开,则操作系统会在进程退出时释放进程内存,但保留有关进程的管理信息,例如句柄、退出代码和退出时间。 若要获取此信息,可以使用 ExitCode ExitTime 属性。 这些属性会自动填充此组件启动的进程。 当与系统进程关联的所有 Process 组件被销毁并不再保留退出进程的句柄时,将释放管理信息。

进程可以独立于代码终止。 如果使用此组件启动进程,系统会自动更新值 HasExited ,即使关联的进程独立退出也是如此。

当标准输出被重定向到异步事件处理程序时,当此属性返回 true 时,输出处理可能尚未完成。 若要确保异步事件处理已完成,请在检查 HasExited 之前调用 WaitForExit() 不带参数的重载。