Process.HasExited 属性

定义

获取指示关联进程是否已终止的值。Gets a value indicating whether the associated process has been terminated.

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

属性值

Boolean

如果 Process 组件引用的操作系统进程已终止,则为 true;否则为 falsetrue if the operating system process referenced by the Process component has terminated; otherwise, false.

属性

例外

没有与此对象关联的进程。There is no process associated with the object.

无法检索该进程的退出代码。The exit code for the process could not be retrieved.

你正尝试访问在远程计算机上运行的进程的 HasExited 属性。You are trying to access the HasExited property for a process that is running on a remote computer. 此属性仅可用于本地计算机上运行的进程。This property is available only for processes that are running on the local computer.

示例

下面的示例启动记事本的实例。The following example starts an instance of Notepad. 然后,它以2秒的间隔检索关联进程的物理内存使用率,最大值为10秒。It then retrieves the physical memory usage of the associated process at 2 second intervals for a maximum of 10 seconds. 该示例检测进程是否在10秒后退出。The example detects whether the process exits before 10 seconds have elapsed. 如果10秒后仍在运行,则此示例将关闭进程。The example closes the process if it is still running after 10 seconds.

#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

注解

如果值 true 为, HasExited 则指示关联进程已终止,通常为或异常终止。A value of true for HasExited indicates that the associated process has terminated, either normally or abnormally. 可以通过调用或来请求或强制关联进程退出 CloseMainWindow KillYou can request or force the associated process to exit by calling CloseMainWindow or Kill. 如果句柄已打开到进程,则在进程退出后,操作系统将释放进程内存,但会保留有关进程的管理信息,例如句柄、退出代码和退出时间。If a handle is open to the process, the operating system releases the process memory when the process has exited, but retains administrative information about the process, such as the handle, exit code, and exit time. 若要获取此信息,你可以使用 ExitCodeExitTime 属性。To get this information, you can use the ExitCode and ExitTime properties. 对于此组件启动的进程,会自动填充这些属性。These properties are populated automatically for processes that were started by this component. 当所有 Process 与系统进程关联的组件都被销毁并且不将更多的句柄保留到已退出的进程时,管理信息将被释放。The administrative information is released when all the Process components that are associated with the system process are destroyed and hold no more handles to the exited process.

进程可以独立于你的代码而终止。A process can terminate independently of your code. 如果使用此组件启动了进程,则系统会自动更新的值 HasExited ,即使关联的进程独立退出也是如此。If you started the process using this component, the system updates the value of HasExited automatically, even if the associated process exits independently.

备注

将标准输出重定向到异步事件处理程序后,当此属性返回时,输出处理可能不会完成 trueWhen standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this property returns true. 若要确保异步事件处理已完成,请 WaitForExit() 在检查前调用不带任何参数的重载 HasExitedTo ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter before checking HasExited.

适用于

另请参阅