Process.Exited Process.Exited Process.Exited Process.Exited Event


Occurs when a process exits.

 event EventHandler ^ Exited;
public event EventHandler Exited;
member this.Exited : EventHandler 
Public Custom Event Exited As EventHandler 


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.

using System;
using System.Diagnostics;
using System.Threading;

class PrintProcessClass
    private Process myProcess;
    private int elapsedTime;
    private bool eventHandled;

    // Print a file with any known extension.
    public void PrintDoc(string fileName)
        elapsedTime = 0;
        eventHandled = false;

        using (myProcess = new Process())
                // 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;
                myProcess.Exited += new EventHandler(myProcess_Exited);
            catch (Exception ex)
                Console.WriteLine($"An error occurred trying to print \"{fileName}\":\n{ex.Message}");

            // Wait for Exited event, but not more than 30 seconds.
            const int SleepAmount = 100;
            while (!eventHandled)
                elapsedTime += SleepAmount;
                if (elapsedTime > 30000)

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)
        eventHandled = true;
            $"Exit time    : {myProcess.ExitTime}\n" +
            $"Exit code    : {myProcess.ExitCode}\n" +
            $"Elapsed time : {elapsedTime}");

    public static void Main(string[] args)
        // Verify that an argument has been entered.
        if (args.Length <= 0)
            Console.WriteLine("Enter a file name.");

        // Create the process and print the document.
        PrintProcessClass myPrintProcess = new PrintProcessClass();
Imports System.Diagnostics
Imports System.Threading

Class PrintProcessClass

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

    Public Event Exited As EventHandler

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

        elapsedTime = 0
        eventHandled = False

        Using myProcess = New Process
                ' 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
        End Using

        ' 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 myPrintProcess As New PrintProcessClass
    End Sub
End Class


The Exited event indicates that the associated process exited. This occurrence means either that the process terminated (aborted) or successfully closed. This event can occur only if the value of the EnableRaisingEvents property is true.

There are two ways of being notified when the associated process exits: synchronously and asynchronously. Synchronous notification means calling the WaitForExit method to block the current thread until the process exits. Asynchronous notification uses the Exited event, which allows the calling thread to continue execution in the meantime. In the latter case, EnableRaisingEvents must be set to true for the calling application to receive the Exited event.

When the operating system shuts down a process, it notifies all other processes that have registered handlers for the Exited event. At this time, the handle of the process that just exited can be used to access some properties such as ExitTime and HasExited that the operating system maintains until it releases that handle completely.


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.

For more information about the use of the Exited event in Windows Forms applications, see the SynchronizingObject property.


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

Applies to