Process.Exited Event

Occurs when a process exits.

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

public event EventHandler Exited
/** @event */
public void add_Exited (EventHandler value)

/** @event */
public void remove_Exited (EventHandler value)

JScript supports the use of events, but not the declaration of new ones.

The Exited event indicates that the associated process exited. This occurrence means either that the process terminated (aborted) or successfully closed.

There are two ways of being notified when the associated process exits: synchronously and asynchronously. Synchronous notification relies on calling the WaitForExit method to pause the processing of your application until the associated component exits. Asynchronous notification relies on the Exited event. In either case, EnableRaisingEvents must be set to true for the Process component to receive notification that the process has exited.

When the operating system shuts down a process, any process component that is waiting for an exit is notified. The component can then access the associated process information that is still resident in the operating system memory (such as ExitTime property) by using the handle that it has to the process.

Because the associated process has exited, the Handle property of the component no longer points to an existing process resource. Instead, it can be used only to access the operating system's information about the process resource. The system is aware of handles to exited processes that have not been released by Process components, so it keeps the ExitTime and Handle property information in memory until the Process component specifically frees the resources.


Even if you have a handle to an exited process, you cannot call Start again to reconnect to the same process. Calling Start automatically releases the associated process and connects to a process with the same file but an entirely new Handle.

The following code example creates a process that prints a file. It raises the Exited event when the process exits because the EnableRaisingEvents property was set when the process was created. The Exited event handler displays process information.

Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Threading
Imports Microsoft.VisualBasic

Class PrintProcessClass

    Private WithEvents myProcess As New Process
    Private elapsedTime As Integer
    Private eventHandled As Boolean

    ' Print a file with any known extension.
    Sub PrintDoc(ByVal fileName As String)

        elapsedTime = 0
        eventHandled = False

            ' Start a process to print a file and raise an event when done.
            myProcess.StartInfo.FileName = fileName
            myProcess.StartInfo.Verb = "Print"
            myProcess.StartInfo.CreateNoWindow = True
            myProcess.EnableRaisingEvents = True

        Catch ex As Exception
            Console.WriteLine("An error occurred trying to print ""{0}"":" & _
                vbCrLf & ex.Message, fileName)
        End Try

        ' Wait for Exited event, but not more than 30 seconds.
        Const SLEEP_AMOUNT As Integer = 100
        Do While Not eventHandled
            elapsedTime += SLEEP_AMOUNT
            If elapsedTime > 30000 Then
                Exit Do
            End If
    End Sub

    ' Handle Exited event and display process information.
    Private Sub myProcess_Exited(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles myProcess.Exited

        eventHandled = True
        Console.WriteLine("Exit time:    {0}" & vbCrLf & _
            "Exit code:    {1}" & vbCrLf & "Elapsed time: {2}", _
            myProcess.ExitTime, myProcess.ExitCode, elapsedTime)
    End Sub

    Shared Sub Main(ByVal args() As String)

        ' Verify that an argument has been entered.
        If args.Length <= 0 Then
            Console.WriteLine("Enter a file name.")
        End If

        ' Create the process and print the document.
        Dim myProcess As New PrintProcessClass
    End Sub
End Class

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0