Export (0) Print
Expand All

Process.HasExited Property

Gets a value indicating whether the associated process has been terminated.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

'Declaration
<BrowsableAttribute(False)> _
<MonitoringDescriptionAttribute("ProcessTerminated")> _
Public ReadOnly Property HasExited As Boolean

Property Value

Type: System.Boolean
true if the operating system process referenced by the Process component has terminated; otherwise, false.

ExceptionCondition
InvalidOperationException

There is no process associated with the object.

Win32Exception

The exit code for the process could not be retrieved.

NotSupportedException

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.

A value of true for HasExited indicates that the associated process has terminated, either normally or abnormally. You 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. 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. 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. If you started the process using this component, the system updates the value of HasExited automatically, even if the associated process exits independently.

NoteNote

When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this property returns true. To ensure that asynchronous event handling has been completed, call the WaitForExit overload that takes no parameter before checking HasExited.

The following example starts an instance of Notepad. It then retrieves the physical memory usage of the associated process at 2 second intervals for a maximum of 10 seconds. The example detects whether the process exits before 10 seconds have elapsed. The example closes the process if it is still running after 10 seconds.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

  • LinkDemand 

    for full trust for the immediate caller. This member cannot be used by partially trusted code.

Show:
© 2015 Microsoft