Process.ExitTime Process.ExitTime Process.ExitTime Process.ExitTime Property

定義

関連付けられたプロセスが終了した時刻を取得します。Gets the time that the associated process exited.

public:
 property DateTime ExitTime { DateTime get(); };
[System.ComponentModel.Browsable(false)]
public DateTime ExitTime { get; }
member this.ExitTime : DateTime
Public ReadOnly Property ExitTime As DateTime

プロパティ値

関連付けられたプロセスが終了した時刻を示す DateTimeA DateTime that indicates when the associated process was terminated.

例外

リモート コンピューターで実行中のプロセスの ExitTime プロパティにアクセスしようとしています。You are trying to access the ExitTime property for a process that is running on a remote computer. このプロパティはローカル コンピューターで実行中のプロセスに対してのみ使用可能です。This property is available only for processes that are running on the local computer.

次のコード例では、ファイルを出力するプロセスを作成します。The following code example creates a process that prints a file. プロセスは、終了Exited時にイベントを発生ExitTimeさせ、イベントハンドラーはプロパティとその他のプロセス情報を表示します。The process raises the Exited event when it exits, and the event handler displays the ExitTime property and other 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())
        {
            try
            {
                // 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);
                myProcess.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred trying to print \"{fileName}\":\n{ex.Message}");
                return;
            }

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

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        eventHandled = true;
        Console.WriteLine(
            $"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.");
            return;
        }

        // Create the process and print the document.
        PrintProcessClass myPrintProcess = new PrintProcessClass();
        myPrintProcess.PrintDoc(args[0]);
    }
}
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
            Try
                ' 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.Start()

            Catch ex As Exception
                Console.WriteLine("An error occurred trying to print ""{0}"":" &
                vbCrLf & ex.Message, fileName)
                Return
            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
            Thread.Sleep(SLEEP_AMOUNT)
        Loop
    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.")
            Return
        End If

        ' Create the process and print the document.
        Dim myPrintProcess As New PrintProcessClass
        myPrintProcess.PrintDoc(args(0))
    End Sub
End Class

注釈

プロセスが終了していない場合、 ExitTimeプロパティを取得しようとすると例外がスローされます。If the process has not terminated, attempting to retrieve the ExitTime property throws an exception. プロパティExitTimeを取得する前にを使用HasExitedして、関連付けられたプロセスが終了したかどうかを確認します。Use HasExited before getting the ExitTime property to determine whether the associated process has terminated.

セキュリティ

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

適用対象

こちらもご覧ください