関連付けられたプロセスが終了したときにプロセスによって指定された値を取得します。
Public ReadOnly Property ExitCode As Integer
[C#]
public int ExitCode {get;}
[C++]
public: __property int get_ExitCode();
[JScript]
public function get ExitCode() : int;
プロパティ値
関連付けられたプロセスが終了したときにプロセスによって指定されたコード。
例外
例外の種類 | 条件 |
---|---|
InvalidOperationException | プロセスが終了していません。
または プロセス Handle が無効です。 |
解説
ExitCode を使用して、システム プロセスの終了時に返されたステータスを取得します。終了コードは、 main()
プロシージャからの整数型戻り値と同じように使用できます。実装方法は、アプリケーションによって異なります。
開発者は通常、ゼロの ExitCode 値で正常終了を表し、0 以外の値でエラーを表します。この 0 以外の値を使用して、呼び出し元のメソッドはプロセスの異常終了の原因を識別できます。これらのガイドラインに従う必要はありませんが、慣習的にこのようになっています。
プロセスが終了する前に ExitCode を取得しようとすると、例外がスローされます。まず HasExited プロパティを調べて、関連付けられたプロセスが終了したかどうかを検査してください。
CloseMainWindow メソッドまたは Kill メソッドを使用して、関連付けられたプロセスを終了させることができます。
関連付けられたプロセスが終了したときの通知方法には、同期と非同期の 2 つの方法があります。同期通知は、関連付けられたコンポーネントが終了するまでアプリケーションの処理を一時中断する WaitForExit メソッド呼び出しに依存します。非同期通知は、 Exited イベントに依存します。どちらの場合も、プロセスが終了したという通知を Process コンポーネントが受け取ることができるように、 EnableRaisingEvents を true に設定する必要があります。
使用例
[Visual Basic, C#, C++] メモ帳のインスタンスを起動する例を次に示します。この例では、インスタンスの起動後、関連付けられているプロセスのさまざまなプロパティが取得および表示されます。その後、プロセスの終了を検知し、プロセスの終了コードを表示します。
Imports System
Imports System.Diagnostics
Imports System.Threading
Namespace Process_Sample
Class MyProcessClass
Public Shared Sub Main()
Try
Dim myProcess As Process
myProcess = Process.Start("NotePad.exe")
While Not myProcess.HasExited
Console.WriteLine()
' Get physical memory usage of the associated process.
Console.WriteLine("Process's physical memory usage: " + _
myProcess.WorkingSet.ToString)
' Get base priority of the associated process.
Console.WriteLine("Base priority of the associated process: " + _
myProcess.BasePriority.ToString)
' Get priority class of the associated process.
Console.WriteLine("Priority class of the associated process: " + _
myProcess.PriorityClass.ToString)
' Get user processor time for this process.
Console.WriteLine("User Processor Time: " + _
myProcess.UserProcessorTime.ToString)
' Get privileged processor time for this process.
Console.WriteLine("Privileged Processor Time: " + _
myProcess.PrivilegedProcessorTime.ToString)
' Get total processor time for this process.
Console.WriteLine("Total Processor Time: " + _
myProcess.TotalProcessorTime.ToString)
' Invoke overloaded ToString function.
Console.WriteLine("Process's Name: " + myProcess.ToString)
Console.WriteLine("-------------------------------------")
If myProcess.Responding Then
Console.WriteLine("Status: Responding to user interface")
myProcess.Refresh()
Else
Console.WriteLine("Status: Not Responding")
End If
Thread.Sleep(1000)
End While
Console.WriteLine()
Console.WriteLine("Process exit code: {0}", myProcess.ExitCode)
Catch e As Exception
Console.WriteLine("The following exception was raised: " + e.Message)
End Try
End Sub 'Main
End Class 'MyProcessClass
End Namespace 'Process_Sample
[C#]
using System;
using System.Diagnostics;
using System.Threading;
namespace Process_Sample
{
class MyProcessClass
{
public static void Main()
{
try
{
Process myProcess;
myProcess = Process.Start("NotePad.exe");
while(!myProcess.HasExited)
{
Console.WriteLine();
// Get physical memory usage of the associated process.
Console.WriteLine("Process's physical memory usage: " + myProcess.WorkingSet);
// Get base priority of the associated process.
Console.WriteLine("Base priority of the associated process: " + myProcess.BasePriority);
// Get priority class of the associated process.
Console.WriteLine("Priority class of the associated process: " + myProcess.PriorityClass);
// Get user processor time for this process.
Console.WriteLine("User Processor Time: " + myProcess.UserProcessorTime);
// Get privileged processor time for this process.
Console.WriteLine("Privileged Processor Time: " + myProcess.PrivilegedProcessorTime);
// Get total processor time for this process.
Console.WriteLine("Total Processor Time: " + myProcess.TotalProcessorTime);
// Invoke overloaded ToString function.
Console.WriteLine("Process's Name: " + myProcess.ToString());
Console.WriteLine("-------------------------------------");
if(myProcess.Responding)
{
Console.WriteLine("Status: Responding to user interface");
myProcess.Refresh();
}
else
{
Console.WriteLine("Status: Not Responding");
}
Thread.Sleep(1000);
}
Console.WriteLine();
Console.WriteLine("Process exit code: {0}", myProcess.ExitCode);
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised: " + e.Message);
}
}
}
}
[C++]
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main() {
try {
Process* myProcess;
myProcess = Process::Start(S"NotePad.exe");
while(!myProcess->HasExited)
{
Console::WriteLine();
// Get physical memory usage of the associated process.
Console::WriteLine(S"Process's physical memory usage: {0}", myProcess->WorkingSet.ToString());
// Get base priority of the associated process.
Console::WriteLine(S"Base priority of the associated process: {0}", myProcess->BasePriority.ToString());
// Get priority class of the associated process.
Console::WriteLine("Priority class of the associated process: {0}", __box(myProcess->PriorityClass));
// Get user processor time for this process.
Console::WriteLine(S"User Processor Time: {0}", myProcess->UserProcessorTime.ToString());
// Get privileged processor time for this process.
Console::WriteLine(S"Privileged Processor Time: {0}", myProcess->PrivilegedProcessorTime.ToString());
// Get total processor time for this process.
Console::WriteLine(S"Total Processor Time: {0}", myProcess->TotalProcessorTime.ToString());
// Invoke overloaded ToString function.
Console::WriteLine(S"Process's Name: {0}", myProcess->ToString());
Console::WriteLine(S"-------------------------------------");
if (myProcess->Responding)
{
Console::WriteLine(S"Status: Responding to user interface");
myProcess->Refresh();
}
else
{
Console::WriteLine(S"Status: Not Responding");
}
Thread::Sleep(1000);
}
Console::WriteLine();
Console::WriteLine("Process exit code: {0}", myProcess->ExitCode.ToString());
} catch (Exception* e) {
Console::WriteLine(S"The following exception was raised: {0}", e->Message);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- SecurityPermission (完全信頼を指定して System.Diagnostic.Process のメンバを呼び出すためのアクセス許可) PermissionState.Unrestricted (関連する列挙体)
参照
Process クラス | Process メンバ | System.Diagnostics 名前空間 | HasExited | CloseMainWindow | Kill | WaitForExit | EnableRaisingEvents