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

属性值

如果组件引用的 操作系统进程已终止,则为

属性

例外

没有与对象关联的进程。

无法检索进程的退出代码。

你正在尝试访问 HasExited 远程计算机上运行的进程的属性。 此属性仅适用于在本地计算机上运行的进程。

示例

以下示例启动记事本的实例。 然后,它会以 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 如果句柄对进程打开,则操作系统会在进程退出时释放进程内存,但保留有关进程的管理信息,例如句柄、退出代码和退出时间。 若要获取此信息,可以使用和ExitCodeExitTime属性。 这些属性会自动填充此组件启动的进程。 当与系统进程关联的所有 Process 组件被销毁并不再保留退出进程的句柄时,将释放管理信息。

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

注释

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

适用于

另请参阅