Process.HasExited プロパティ

定義

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

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

プロパティ値

Boolean

Process コンポーネントが参照するオペレーティング システム プロセスが終了している場合は true。それ以外の場合は false

属性

例外

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

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

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

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

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   try
   {
      Process^ myProcess;
      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 : {0}", myProcess->WorkingSet.ToString() );
            
            // Wait 2 seconds.
            Thread::Sleep( 2000 );
         }
         else
         {
            break;
         }

      }
      myProcess->CloseMainWindow();
      
      // Free resources associated with process.
      myProcess->Close();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The following exception was raised: " );
      Console::WriteLine( e->Message );
   }

}
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);
            }
        }
    }
}
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

注釈

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

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

注意

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

適用対象

こちらもご覧ください