This documentation is archived and is not being maintained.

Process.OnExited Method

Updated: October 2009

Raises the Exited event.

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

protected void OnExited()

OnExited is the API method that raises the Exited event. Calling OnExited causes the Exited event to occur and is the only way to raise the event using the Process component. OnExited is primarily used when deriving classes from the component.

As an alternative to OnExited, you can write your own event handler. You create your own event handler delegate and your own event-handling method.

NoteNote:

If you are using the Visual Studio environment, an event handler delegate (AddOnExited) and an event-handling method (Process1_Exited) are created for you when you drag a Process component onto a form and double-click the icon. The code you create to run when the Exited event occurs is entered into the Process1_Exited procedure. You do not need to create the OnExited member, because it is implemented for you.

Raising an event invokes the event handler through a delegate. For an overview, see Raising an Event.

The following example shows how to use the OnExited method in a derived class.

using System;
using System.Diagnostics;

class MyProcess : Process
{
    public void Stop()
    {
        this.CloseMainWindow();
        this.Close();
        OnExited();
    } 
}
class StartNotePad
{

    public static void Main(string[] args)
    {
        MyProcess p = new MyProcess();
        p.StartInfo.FileName = "notepad.exe";
        p.EnableRaisingEvents = true;
        p.Exited += new EventHandler(myProcess_HasExited);
        p.Start();
        p.WaitForInputIdle();
        p.Stop();
    }
    private static void myProcess_HasExited(object sender, System.EventArgs e)
    {
        Console.WriteLine("Process has exited.");
    }
}

  • LinkDemand 

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

Date

History

Reason

October 2009

Added example.

Customer feedback.

Show: