Process.OnExited Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Raises the Exited event.
protected:
void OnExited();
protected void OnExited ();
member this.OnExited : unit -> unit
Protected Sub OnExited ()
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.");
}
}
open System
open System.Diagnostics
type MyProcess() =
inherit Process()
member this.Stop() =
this.CloseMainWindow() |> ignore
this.Close()
this.OnExited()
let myProcess_HasExited (sender: obj) (e: EventArgs) = printfn "Process has exited."
let p = new MyProcess()
p.StartInfo.FileName <- "notepad.exe"
p.EnableRaisingEvents <- true
p.Exited.AddHandler(EventHandler myProcess_HasExited)
p.Start() |> ignore
p.WaitForInputIdle() |> ignore
p.Stop()
Imports System.Diagnostics
Class MyProcess
Inherits Process
Public Sub [Stop]()
Me.CloseMainWindow()
Me.Close()
OnExited()
End Sub
End Class
Class StartNotePad
Public Shared Sub Main(ByVal args() As String)
Dim p As New MyProcess()
p.StartInfo.FileName = "notepad.exe"
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf myProcess_HasExited
p.Start()
p.WaitForInputIdle()
p.Stop()
End Sub
Private Shared Sub myProcess_HasExited(ByVal sender As Object, ByVal e As System.EventArgs)
Console.WriteLine("Process has exited.")
End Sub
End Class
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.
Note
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 Handling and Raising Events.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: