Process.Exited Process.Exited Process.Exited Process.Exited Event


プロセスが終了したときに発生します。Occurs when a process exits.

 event EventHandler ^ Exited;
public event EventHandler Exited;
member this.Exited : EventHandler 
Public Custom Event Exited As EventHandler 

次のコード例では、ファイルを出力するプロセスを作成します。The following code example creates a process that prints a file. 生成、Exitedため、プロセスが終了するときにイベントをEnableRaisingEventsプロセスの作成時にプロパティが設定されました。It raises the Exited event when the process exits because the EnableRaisingEvents property was set when the process was created. Exitedイベント ハンドラーは、プロセス情報を表示します。The Exited event handler displays process information.

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

class PrintProcessClass
    private Process myProcess;
    private int elapsedTime;
    private bool eventHandled;

    // Print a file with any known extension.
    public void PrintDoc(string fileName)
        elapsedTime = 0;
        eventHandled = false;

        using (myProcess = new Process())
                // 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 \"{fileName}\":\n{ex.Message}");

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

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)
        eventHandled = true;
            $"Exit time    : {myProcess.ExitTime}\n" +
            $"Exit code    : {myProcess.ExitCode}\n" +
            $"Elapsed time : {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();
Imports System
Imports System.Diagnostics
Imports System.Threading

Class PrintProcessClass

    Private WithEvents myProcess As Process
    Private elapsedTime As Integer
    Private eventHandled As Boolean

    Public Event Exited As EventHandler

    ' Print a file with any known extension.
    Sub PrintDoc(ByVal fileName As String)

        elapsedTime = 0
        eventHandled = False

        Using myProcess = New Process
                ' 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

            Catch ex As Exception
                Console.WriteLine("An error occurred trying to print ""{0}"":" &
                vbCrLf & ex.Message, fileName)
            End Try
        End Using

        ' Wait for Exited event, but not more than 30 seconds.
        Const SLEEP_AMOUNT As Integer = 100
        Do While Not eventHandled
            elapsedTime += SLEEP_AMOUNT
            If elapsedTime > 30000 Then
                Exit Do
            End If
    End Sub

    ' Handle Exited event and display process information.
    Private Sub myProcess_Exited(ByVal sender As Object,
            ByVal e As System.EventArgs) Handles myProcess.Exited

        eventHandled = True
        Console.WriteLine("Exit time:    {0}" & vbCrLf &
            "Exit code:    {1}" & vbCrLf & "Elapsed time: {2}",
            myProcess.ExitTime, myProcess.ExitCode, elapsedTime)
    End Sub

    Shared Sub Main(ByVal args() As String)

        ' Verify that an argument has been entered.
        If args.Length <= 0 Then
            Console.WriteLine("Enter a file name.")
        End If

        ' Create the process and print the document.
        Dim myPrintProcess As New PrintProcessClass
    End Sub
End Class


Exitedイベントは、関連付けられたプロセスが終了していることを示します。The Exited event indicates that the associated process exited. この状況の発生を意味するか、プロセス終了 (中止) または正常に終了します。This occurrence means either that the process terminated (aborted) or successfully closed. 場合にのみ、このイベントが発生する可能性がの値、EnableRaisingEventsプロパティはtrueします。This event can occur only if the value of the EnableRaisingEvents property is true.

関連付けられたプロセスが終了したときに通知する 2 つの方法がある: 同期的および非同期的にします。There are two ways of being notified when the associated process exits: synchronously and asynchronously. 同期の通知呼び出し元の意味、WaitForExitプロセスを終了するまで、現在のスレッドをブロックするメソッド。Synchronous notification means calling the WaitForExit method to block the current thread until the process exits. 非同期通知を使用して、Exitedイベントで、呼び出し元スレッドがその間に実行を続行することができます。Asynchronous notification uses the Exited event, which allows the calling thread to continue execution in the meantime. 後者の場合、EnableRaisingEventsに設定する必要がありますtrueExited イベントを受信する呼び出し元のアプリケーション。In the latter case, EnableRaisingEvents must be set to true for the calling application to receive the Exited event.

オペレーティング システムをシャット ダウン、プロセス、Exited イベントのハンドラーを登録している他のすべてのプロセスを通知します。When the operating system shuts down a process, it notifies all other processes that have registered handlers for the Exited event. この時点でだけが終了したプロセスのハンドルを使用してなど一部のプロパティにアクセスするExitTimeHasExitedそのハンドルを完全に解放するまで、オペレーティング システムが維持されます。At this time, the handle of the process that just exited can be used to access some properties such as ExitTime and HasExited that the operating system maintains until it releases that handle completely.


呼び出すことはできません、終了プロセスを識別するハンドルがある場合でもStart再接続するには、同じプロセスです。Even if you have a handle to an exited process, you cannot call Start again to reconnect to the same process. 呼び出すStart自動的に関連付けられたプロセスを解放し、同じファイルが完全に新しいプロセスに接続Handleします。Calling Start automatically releases the associated process and connects to a process with the same file but an entirely new Handle.

使用の詳細については、 Exited Windows フォーム アプリケーションでイベントを参照してください、SynchronizingObjectプロパティ。For more information about the use of the Exited event in Windows Forms applications, see the SynchronizingObject property.


直前の呼び出し元に対する完全な信頼。for full trust for the immediate caller. このメンバーは、部分的に信頼されているコードから使用することはできません。This member cannot be used by partially trusted code.