Process.ExitCode プロパティ

定義

関連付けられたプロセスが終了したときにプロセスによって指定された値を取得します。Gets the value that the associated process specified when it terminated.

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

プロパティ値

関連付けられたプロセスが終了したときにプロセスによって指定されたコード。The code that the associated process specified when it terminated.

属性

例外

プロセスは終了していません。The process has not exited.

- または --or- プロセス Handle は終了していません。The process Handle is not valid.

リモート コンピューターで実行中のプロセスの ExitCode プロパティにアクセスしようとしています。You are trying to access the ExitCode 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 example starts an instance of Notepad. 次に、この例では、関連付けられているプロセスのさまざまなプロパティを取得して表示します。The example then retrieves and displays various properties of the associated process. この例では、プロセスが終了したことを検出し、プロセスの終了コードを表示します。The example detects when the process exits, and displays the process's exit code.

#using <system.dll>

using namespace System;
using namespace System::Diagnostics;
int main()
{
   
   // Define variables to track the peak
   // memory usage of the process.
   _int64 peakPagedMem = 0,peakWorkingSet = 0,peakVirtualMem = 0;
   Process^ myProcess = nullptr;
   try
   {
      
      // Start the process.
      myProcess = Process::Start( "NotePad.exe" );
      
      // Display the process statistics until
      // the user closes the program.
      do
      {
         if (  !myProcess->HasExited )
         {
            
            // Refresh the current process property values.
            myProcess->Refresh();
            Console::WriteLine();
            
            // Display current process statistics.
            Console::WriteLine( "{0} -", myProcess );
            Console::WriteLine( "-------------------------------------" );
            Console::WriteLine( "  physical memory usage: {0}", myProcess->WorkingSet64 );
            Console::WriteLine( "  base priority: {0}", myProcess->BasePriority );
            Console::WriteLine( "  priority class: {0}", myProcess->PriorityClass );
            Console::WriteLine( "  user processor time: {0}", myProcess->UserProcessorTime );
            Console::WriteLine( "  privileged processor time: {0}", myProcess->PrivilegedProcessorTime );
            Console::WriteLine( "  total processor time: {0}", myProcess->TotalProcessorTime );
            Console::WriteLine("  PagedSystemMemorySize64: {0}", myProcess->PagedSystemMemorySize64);
            Console::WriteLine("  PagedMemorySize64: {0}", myProcess->PagedMemorySize64);
            
            // Update the values for the overall peak memory statistics.
            peakPagedMem = myProcess->PeakPagedMemorySize64;
            peakVirtualMem = myProcess->PeakVirtualMemorySize64;
            peakWorkingSet = myProcess->PeakWorkingSet64;
            if ( myProcess->Responding )
            {
               Console::WriteLine( "Status = Running" );
            }
            else
            {
               Console::WriteLine( "Status = Not Responding" );
            }
         }
      }
      while (  !myProcess->WaitForExit( 1000 ) );
      Console::WriteLine();
      Console::WriteLine( "Process exit code: {0}", myProcess->ExitCode );
      
      // Display peak memory statistics for the process.
      Console::WriteLine( "Peak physical memory usage of the process: {0}", peakWorkingSet );
      Console::WriteLine( "Peak paged memory usage of the process: {0}", peakPagedMem );
      Console::WriteLine( "Peak virtual memory usage of the process: {0}", peakVirtualMem );
   }
   finally
   {
      if ( myProcess != nullptr )
      {
         myProcess->Close();
      }
   }

}
using System;
using System.Diagnostics;

namespace ProcessSample
{
    class ProcessMonitorSample
    {
        public static void Main()
        {
            // Define variables to track the peak
            // memory usage of the process.
            long peakPagedMem   = 0,
                 peakWorkingSet = 0,
                 peakVirtualMem = 0;

            // Start the process.
            using (Process myProcess = Process.Start("NotePad.exe"))
            {
                // Display the process statistics until
                // the user closes the program.
                do
                {
                    if (!myProcess.HasExited)
                    {
                        // Refresh the current process property values.
                        myProcess.Refresh();

                        Console.WriteLine();

                        // Display current process statistics.

                        Console.WriteLine($"{myProcess} -");
                        Console.WriteLine("-------------------------------------");

                        Console.WriteLine($"  Physical memory usage     : {myProcess.WorkingSet64}");
                        Console.WriteLine($"  Base priority             : {myProcess.BasePriority}");
                        Console.WriteLine($"  Priority class            : {myProcess.PriorityClass}");
                        Console.WriteLine($"  User processor time       : {myProcess.UserProcessorTime}");
                        Console.WriteLine($"  Privileged processor time : {myProcess.PrivilegedProcessorTime}");
                        Console.WriteLine($"  Total processor time      : {myProcess.TotalProcessorTime}");
                        Console.WriteLine($"  Paged system memory size  : {myProcess.PagedSystemMemorySize64}");
                        Console.WriteLine($"  Paged memory size         : {myProcess.PagedMemorySize64}");

                        // Update the values for the overall peak memory statistics.
                        peakPagedMem   = myProcess.PeakPagedMemorySize64;
                        peakVirtualMem = myProcess.PeakVirtualMemorySize64;
                        peakWorkingSet = myProcess.PeakWorkingSet64;

                        if (myProcess.Responding)
                        {
                            Console.WriteLine("Status = Running");
                        }
                        else
                        {
                            Console.WriteLine("Status = Not Responding");
                        }
                    }
                }
                while (!myProcess.WaitForExit(1000));


                Console.WriteLine();
                Console.WriteLine($"  Process exit code          : {myProcess.ExitCode}");

                // Display peak memory statistics for the process.
                Console.WriteLine($"  Peak physical memory usage : {peakWorkingSet}");
                Console.WriteLine($"  Peak paged memory usage    : {peakPagedMem}");
                Console.WriteLine($"  Peak virtual memory usage  : {peakVirtualMem}");
            }
        }
    }
}
Imports System.Diagnostics

Namespace ProcessSample
    Class ProcessMonitorSample

        Public Shared Sub Main()

            ' Define variables to track the peak
            ' memory usage of the process.
            Dim peakPagedMem As Long = 0
            Dim peakWorkingSet As Long = 0
            Dim peakVirtualMem As Long = 0

            ' Start the process.
            Using myProcess = Process.Start("NotePad.exe")

                ' Display process statistics until
                ' the user closes the program.
                Do

                    If Not myProcess.HasExited Then

                        ' Refresh the current process property values.
                        myProcess.Refresh()

                        Console.WriteLine()

                        ' Display current process statistics.

                        Console.WriteLine($"{myProcess} -")
                        Console.WriteLine("-------------------------------------")

                        Console.WriteLine($"  Physical memory usage     : {myProcess.WorkingSet64}")
                        Console.WriteLine($"  Base priority             : {myProcess.BasePriority}")
                        Console.WriteLine($"  Priority class            : {myProcess.PriorityClass}")
                        Console.WriteLine($"  User processor time       : {myProcess.UserProcessorTime}")
                        Console.WriteLine($"  Privileged processor time : {myProcess.PrivilegedProcessorTime}")
                        Console.WriteLine($"  Total processor time      : {myProcess.TotalProcessorTime}")
                        Console.WriteLine($"  Paged system memory size  : {myProcess.PagedSystemMemorySize64}")
                        Console.WriteLine($"  Paged memory size         : {myProcess.PagedMemorySize64}")

                        ' Update the values for the overall peak memory statistics.
                        peakPagedMem = myProcess.PeakPagedMemorySize64
                        peakVirtualMem = myProcess.PeakVirtualMemorySize64
                        peakWorkingSet = myProcess.PeakWorkingSet64

                        If myProcess.Responding Then
                            Console.WriteLine("Status = Running")
                        Else
                            Console.WriteLine("Status = Not Responding")
                        End If
                    End If
                Loop While Not myProcess.WaitForExit(1000)

                Console.WriteLine()
                Console.WriteLine($"  Process exit code                         : {myProcess.ExitCode}")

                ' Display peak memory statistics for the process.
                Console.WriteLine($"  Peak physical memory usage of the process : {peakWorkingSet}")
                Console.WriteLine($"  Peak paged memory usage of the process    : {peakPagedMem}")
                Console.WriteLine($"  Peak virtual memory usage of the process  : {peakVirtualMem}")
            End Using
        End Sub
    End Class
End Namespace

注釈

システムプロセスが終了したときに返された状態を取得するには、ExitCode を使用します。Use ExitCode to get the status that the system process returned when it exited. 終了コードは、main() プロシージャからの整数の戻り値と同じように使用できます。You can use the exit code much like an integer return value from a main() procedure.

プロセスの ExitCode 値には、そのプロセスのアプリケーション開発者によって実装される特定の規則が反映されます。The ExitCode value for a process reflects the specific convention implemented by the application developer for that process. 終了コード値を使用してコード内で決定を行う場合は、アプリケーションプロセスで使用される終了コード規則がわかっていることを確認してください。If you use the exit code value to make decisions in your code, be sure that you know the exit code convention used by the application process.

通常、開発者は、ExitCode 値がゼロであることを示し、呼び出し元のメソッドが異常なプロセス終了の原因を特定するために使用できる0以外の値によってエラーを指定します。Developers usually indicate a successful exit by an ExitCode value of zero, and designate errors by nonzero values that the calling method can use to identify the cause of an abnormal process termination. これらのガイドラインに従う必要はありませんが、規則です。It is not necessary to follow these guidelines, but they are the convention.

プロセスが終了する前に ExitCode を取得しようとすると、例外がスローされます。If you try to get the ExitCode before the process has exited, the attempt throws an exception. 最初に HasExited プロパティを調べて、関連付けられたプロセスが終了したかどうかを確認します。Examine the HasExited property first to verify whether the associated process has terminated.

注意

標準出力が非同期イベントハンドラーにリダイレクトされた場合、HasExitedtrueを返すと、出力処理が完了しない可能性があります。When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when HasExited returns true. 非同期イベント処理が完了したことを確認するには、HasExitedを確認する前に、パラメーターを取らない WaitForExit() オーバーロードを呼び出します。To ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter before checking HasExited.

CloseMainWindow または Kill メソッドを使用して、関連付けられたプロセスを終了させることができます。You can use the CloseMainWindow or the Kill method to cause an associated process to exit.

関連付けられたプロセスが終了すると、同期的かつ非同期的に通知される2つの方法があります。There are two ways of being notified when the associated process exits: synchronously and asynchronously. 同期通知は、関連するコンポーネントが終了するまで、アプリケーションの処理を一時停止するために WaitForExit メソッドを呼び出すことに依存します。Synchronous notification relies on calling the WaitForExit method to pause the processing of your application until the associated component exits. 非同期通知は、Exited イベントに依存します。Asynchronous notification relies on the Exited event. 非同期通知を使用する場合は、プロセスが終了したことを通知するために、EnableRaisingEventsProcess コンポーネントで true に設定する必要があります。When using asynchronous notification, EnableRaisingEvents must be set to true for the Process component to receive notification that the process has exited.

セキュリティ

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

適用対象

こちらもご覧ください