Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.

Process.Exited (Evento)


Se produce cuando termina un proceso.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

public event EventHandler Exited

The E:System.Diagnostics.Process.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 P:System.Diagnostics.Process.EnableRaisingEvents property is true.

There are two ways of being notified when the associated process exits: synchronously and asynchronously. Synchronous notification means calling the M:System.Diagnostics.Process.WaitForExit(System.Int32) method to block the current thread until the process exits. Asynchronous notification uses the E:System.Diagnostics.Process.Exited event, which allows the calling thread to continue execution in the meantime. In the latter case, P:System.Diagnostics.Process.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 P:System.Diagnostics.Process.ExitTime and P:System.Diagnostics.Process.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 M:System.Diagnostics.Process.Start again to reconnect to the same process. Calling M:System.Diagnostics.Process.Start automatically releases the associated process and connects to a process with the same file but an entirely new P:System.Diagnostics.Process.Handle.

For more information about the use of the E:System.Diagnostics.Process.Exited event in Windows Forms applications, see the P:System.Diagnostics.Process.SynchronizingObject property.

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

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

class PrintProcessClass

    private Process myProcess = new Process();
    private int elapsedTime;
    private bool eventHandled;

    // Print a file with any known extension.
    public void PrintDoc(string fileName)

        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;
            myProcess.Exited += new EventHandler(myProcess_Exited);

        catch (Exception ex)
            Console.WriteLine("An error occurred trying to print \"{0}\":" + "\n" + ex.Message, fileName);

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

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)

        eventHandled = true;
        Console.WriteLine("Exit time:    {0}\r\n" +
            "Exit code:    {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, 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();


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

.NET Framework
Disponible desde 1.1
Volver al principio