Process.HasExited プロパティ

定義

関連付けられているプロセスが終了したかどうかを示す値を取得します。

public:
 property bool HasExited { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool HasExited { get; }
[<System.ComponentModel.Browsable(false)>]
member this.HasExited : bool
Public ReadOnly Property HasExited As Boolean

プロパティ値

true Process コンポーネントによって参照されるオペレーティング システム プロセスが終了した場合は a0/>。それ以外の場合はfalse

属性

例外

オブジェクトに関連付けられているプロセスはありません。

プロセスの終了コードを取得できませんでした。

リモート コンピューターで実行されているプロセスの HasExited プロパティにアクセスしようとしています。 このプロパティは、ローカル コンピューターで実行されているプロセスでのみ使用できます。

次の例では、メモ帳のインスタンスを開始します。 次に、関連付けられているプロセスの物理メモリ使用量を 2 秒間隔で最大 10 秒間取得します。 この例では、10 秒が経過する前にプロセスが終了するかどうかを検出します。 この例では、プロセスが 10 秒後も実行されている場合は、プロセスを閉じます。

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace ProcessSample
{
    class MyProcessClass
    {
        public static void Main()
        {
            try
            {
                using (Process myProcess = Process.Start("Notepad.exe"))
                {
                    // Display physical memory usage 5 times at intervals of 2 seconds.
                    for (int i = 0; i < 5; i++)
                    {
                        if (!myProcess.HasExited)
                        {
                            // Discard cached information about the process.
                            myProcess.Refresh();
                            // Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}");
                            // Wait 2 seconds.
                            Thread.Sleep(2000);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow();
                    // Free resources associated with process.
                    myProcess.Close();
                }
            }
            catch (Exception e) when (e is Win32Exception || e is FileNotFoundException)
            {
                Console.WriteLine("The following exception was raised: ");
                Console.WriteLine(e.Message);
            }
        }
    }
}
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading


try
    use myProcess = Process.Start "Notepad.exe"
    // Display physical memory usage 5 times at intervals of 2 seconds.
    let mutable i = 0

    while i < 5 && not myProcess.HasExited do
        // Discard cached information about the process.
        myProcess.Refresh()
        // Print working set to console.
        printfn $"Physical Memory Usage: {myProcess.WorkingSet64}"
        // Wait 2 seconds.
        Thread.Sleep 2000
        i <- i + 1
    // Close process by sending a close message to its main window.
    myProcess.CloseMainWindow() |> ignore
    // Free resources associated with process.
    myProcess.Close()
with
| :? Win32Exception
| :? FileNotFoundException as e ->
    printfn "The following exception was raised: "
    printfn $"{e.Message}"
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.IO
Imports System.Threading

Namespace Process_Sample
    Class MyProcessClass

        Public Shared Sub Main()
            Try
                Using myProcess = Process.Start("Notepad.exe")
                    ' Display physical memory usage 5 times at intervals of 2 seconds.
                    Dim i As Integer
                    For i = 0 To 4
                        If Not myProcess.HasExited Then

                            ' Discard cached information about the process.
                            myProcess.Refresh()
                            ' Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}")
                            ' Wait 2 seconds.
                            Thread.Sleep(2000)
                        Else
                            Exit For
                        End If

                    Next i

                    ' Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow()
                    ' Free resources associated with process.
                    myProcess.Close()
                End Using
            Catch e As Exception When TypeOf e Is Win32Exception Or TypeOf e Is FileNotFoundException
                Console.WriteLine("The following exception was raised: ")
                Console.WriteLine(e.Message)
            End Try
        End Sub
    End Class
End Namespace 'Process_Sample

注釈

HasExitedの値trueは、関連付けられたプロセスが正常または異常に終了したことを示します。 CloseMainWindowまたはKillを呼び出すことによって、関連付けられているプロセスを要求または強制的に終了できます。 ハンドルがプロセスに対して開かれている場合、オペレーティング システムはプロセスが終了したときにプロセス メモリを解放しますが、ハンドル、終了コード、終了時刻などのプロセスに関する管理情報は保持されます。 この情報を取得するには、 ExitCode プロパティと ExitTime プロパティを使用できます。 これらのプロパティは、このコンポーネントによって開始されたプロセスに対して自動的に設定されます。 管理情報は、システム プロセスに関連付けられているすべての Process コンポーネントが破棄され、終了したプロセスに対するハンドルがこれ以上保持されない場合に解放されます。

プロセスは、コードとは別に終了できます。 このコンポーネントを使用してプロセスを開始した場合、関連するプロセスが独立して終了した場合でも、 HasExited の値が自動的に更新されます。

Note

標準出力が非同期イベント ハンドラーにリダイレクトされている場合、このプロパティが trueを返したときに出力処理が完了しない可能性があります。 非同期イベント処理が完了したことを確認するには、WaitForExit()を確認する前にパラメーターを受け取っていないHasExitedオーバーロードを呼び出します。

適用対象

こちらもご覧ください