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 属性。 此属性仅可用于本地计算机上运行的进程。

示例

以下示例启动记事本的实例。 然后,它每隔 2 秒检索关联进程的物理内存使用情况,最长为 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时,输出处理可能尚未完成。 若要确保异步事件处理已完成,请在 WaitForExit() 检查 之前调用不采用任何参数的 HasExited重载。

适用于

另请参阅