Process.OnExited 方法

定義

引發 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,你可以自行撰寫事件處理程序。 你可以建立自己的事件處理代理和事件處理方法。

Note

如果你使用 Visual Studio 環境,當你將 Process 元件拖到表單並雙擊圖示時,會自動建立事件處理代理(AddOnExited)和事件處理方法(Process1_Exited)。 你建立的執行 Exited 程式碼會輸入到Process1_Exited程序中。 你不需要建立成員, OnExited 因為它是為你實作的。

發起事件會透過代理呼叫事件處理者。 欲了解概述,請參閱 處理與提升事件

適用於

另請參閱