Process.HasExited 属性

获取指示关联进程是否已终止的值。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public ReadOnly Property HasExited As Boolean
用法
Dim instance As Process
Dim value As Boolean

value = instance.HasExited
public bool HasExited { get; }
public:
property bool HasExited {
    bool get ();
}
/** @property */
public boolean get_HasExited ()
public function get HasExited () : boolean

属性值

如果 Process 组件引用的操作系统进程已终止,则为 true;否则为 false

异常

异常类型 条件

InvalidOperationException

没有与该对象关联的进程。

Win32Exception

未能检索进程的退出代码。

备注

HasExited 的值为 true 指示关联的进程已经正常或异常终止。通过调用 CloseMainWindowKill,可以请求或强制关联进程退出。如果句柄对进程是开放的,当进程退出时,操作系统将释放进程内存,但保留有关进程的管理信息,如句柄、退出代码和退出时间。若要获取这些信息,可以使用 ExitCodeExitTime 属性。对于由此组件启动的进程,这些属性是自动填充的。在与系统进程关联的所有 Process 组件均已损坏并且不再持有退出的进程的句柄后,将释放管理信息。

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

示例

下面的示例启动一个记事本实例。然后它最多在 10 秒内,以两秒为间隔检索关联进程的物理内存使用情况。该示例检测该进程在经过 10 秒后是否退出。如果该进程在 10 秒后仍在运行,该示例就会将其关闭。

Imports System
Imports System.Diagnostics
Imports System.Threading

Namespace Process_Sample
   Class MyProcessClass

      Public Shared Sub Main()
         Try

            Dim myProcess As Process
            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.ToString())
                  ' 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()

         Catch e As Exception
            Console.WriteLine("The following exception was raised: ")
            Console.WriteLine(e.Message)
         End Try
      End Sub 'Main
   End Class 'MyProcessClass
End Namespace 'Process_Sample
using System;
using System.Diagnostics;
using System.Threading;

namespace Process_Sample
{
   class MyProcessClass
   {
      public static void 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: " 
                                        + myProcess.WorkingSet.ToString());
                   // 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)
         {
            Console.WriteLine("The following exception was raised: ");
            Console.WriteLine(e.Message);
         }
      }
   }
}
#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 );
   }

}

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Process 类
Process 成员
System.Diagnostics 命名空间
Process.ExitCode 属性
Process.ExitTime 属性
WaitForExit
Process.EnableRaisingEvents 属性
OnExited