Process.ExitTime Właściwość

Definicja

Pobiera czas zakończenia skojarzonego procesu.

public DateTime ExitTime { get; }
[System.ComponentModel.Browsable(false)]
public DateTime ExitTime { get; }

Wartość właściwości

Element DateTime wskazujący, kiedy skojarzony proces został zakończony.

Atrybuty

Wyjątki

Próbujesz uzyskać dostęp do ExitTime właściwości dla procesu uruchomionego na komputerze zdalnym. Ta właściwość jest dostępna tylko dla procesów uruchomionych na komputerze lokalnym.

Przykłady

Poniższy przykład kodu tworzy proces, który drukuje plik. Proces zgłasza zdarzenie po jego zakończeniu Exited , a program obsługi zdarzeń wyświetla ExitTime właściwość i inne informacje o procesie.

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

class PrintProcessClass
{
    private Process myProcess;
    private TaskCompletionSource<bool> eventHandled;

    // Print a file with any known extension.
    public async Task PrintDoc(string fileName)
    {
        eventHandled = new TaskCompletionSource<bool>();

        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.
            await Task.WhenAny(eventHandled.Task,Task.Delay(30000));
        }
    }

    // Handle Exited event and display process information.
    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        Console.WriteLine(
            $"Exit time    : {myProcess.ExitTime}\n" +
            $"Exit code    : {myProcess.ExitCode}\n" +
            $"Elapsed time : {Math.Round((myProcess.ExitTime - myProcess.StartTime).TotalMilliseconds)}");
        eventHandled.TrySetResult(true);
    }

    public static async Task 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();
        await myPrintProcess.PrintDoc(args[0]);
    }
}

Uwagi

Jeśli proces nie został zakończony, próba pobrania ExitTime właściwości zgłasza wyjątek. Użyj HasExited polecenia przed pobraniem właściwości , ExitTime aby określić, czy skojarzony proces został zakończony.

Dotyczy

Zobacz też