Process.OnExited 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
引发 Exited 事件。
protected:
void OnExited();
protected void OnExited();
member this.OnExited : unit -> unit
Protected Sub OnExited ()
示例
以下示例演示如何在派生类中使用 OnExited 该方法。
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 是引发事件的 Exited API 方法。 调用 OnExited 会导致 Exited 事件发生,并且是使用 Process 组件引发事件的唯一方法。 OnExited 主要用于从组件派生类。
作为替代方法 OnExited,可以编写自己的事件处理程序。 创建自己的事件处理程序委托和自己的事件处理方法。
注释
如果使用Visual Studio环境,在将 Process 组件拖动到窗体上时,将创建事件处理程序委托(AddOnExited)和事件处理方法(Process1_Exited)。 在事件发生时 Exited 要运行的代码将输入到Process1_Exited过程中。 无需创建 OnExited 成员,因为它已为你实现。
引发事件会通过委托调用事件处理程序。 有关概述,请参阅 处理和引发事件。