Process.ExitCode 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
연결된 프로세스가 종료될 때 지정한 값을 가져옵니다.
public:
property int ExitCode { int get(); };
public int ExitCode { get; }
[System.ComponentModel.Browsable(false)]
public int ExitCode { get; }
member this.ExitCode : int
[<System.ComponentModel.Browsable(false)>]
member this.ExitCode : int
Public ReadOnly Property ExitCode As Integer
속성 값
연결된 프로세스가 종료될 때 지정한 코드입니다.
- 특성
예외
원격 컴퓨터에서 실행되는 프로세스의 ExitCode 속성에 액세스하려고 합니다. 이 속성은 로컬 컴퓨터에서 실행 중인 프로세스에만 사용할 수 있습니다.
예제
다음 예제에서는 메모장 인스턴스를 시작합니다. 그런 다음, 연결된 프로세스의 다양한 속성을 검색하고 표시합니다. 이 예제에서는 프로세스가 종료되는 시기를 감지하고 프로세스의 종료 코드를 표시합니다.
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}");
}
}
}
}
open System.Diagnostics
// Define variables to track the peak
// memory usage of the process.
let mutable peakPagedMem = 0L
let mutable peakWorkingSet = 0L
let mutable peakVirtualMem = 0L
// Start the process.
use myProcess = Process.Start "NotePad.exe"
// Display the process statistics until
// the user closes the program.
while myProcess.WaitForExit 1000 |> not do
if not myProcess.HasExited then
// Refresh the current process property values.
myProcess.Refresh()
printfn ""
// Display current process statistics.
printfn $"{myProcess} -"
printfn "-------------------------------------"
printfn $" Physical memory usage : {myProcess.WorkingSet64}"
printfn $" Base priority : {myProcess.BasePriority}"
printfn $" Priority class : {myProcess.PriorityClass}"
printfn $" User processor time : {myProcess.UserProcessorTime}"
printfn $" Privileged processor time : {myProcess.PrivilegedProcessorTime}"
printfn $" Total processor time : {myProcess.TotalProcessorTime}"
printfn $" Paged system memory size : {myProcess.PagedSystemMemorySize64}"
printfn $" 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
printfn "Status = Running"
else
printfn "Status = Not Responding"
printfn ""
printfn $" Process exit code : {myProcess.ExitCode}"
// Display peak memory statistics for the process.
printfn $" Peak physical memory usage : {peakWorkingSet}"
printfn $" Peak paged memory usage : {peakPagedMem}"
printfn $" 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 . 프로시저의 정수 반환 값과 마찬가지로 종료 코드를 사용할 수 있습니다 main() .
프로세스의 값은 ExitCode 해당 프로세스에 대해 애플리케이션 개발자가 구현한 특정 규칙을 반영합니다. 출구 코드 값을 사용해 코드를 결정할 때는 애플리케이션 프로세스에서 사용하는 출구 코드 규칙을 반드시 알고 있어야 합니다.
개발자는 일반적으로 0 값으로 성공적인 종료를 ExitCode 나타내고 호출 메서드가 비정상적인 프로세스 종료의 원인을 식별하는 데 사용할 수 있는 0이 아닌 값으로 오류를 지정합니다. 이러한 지침을 따를 필요는 없지만 규칙입니다.
프로세스가 종료되기 전에 가져오기 ExitCode 를 시도하면 예외가 throw됩니다. HasExited 속성을 먼저 검사하여 연결된 프로세스가 종료되었는지 확인합니다.
메모
표준 출력이 비동기 이벤트 처리기로 리디렉션된 경우 반환true될 때 HasExited 출력 처리가 완료되지 않을 수 있습니다. 비동기 이벤트 처리가 완료되었는지 확인하려면 검사하기 전에 매개 변수를 사용하지 않는 오버로드를 호출 WaitForExit() 합니다 HasExited.
또는 메서드를 CloseMainWindowKill 사용하여 연결된 프로세스가 종료되도록 할 수 있습니다.
연결된 프로세스가 종료될 때 알림을 받는 방법에는 동기적 및 비동기적이라는 두 가지 방법이 있습니다. 동기 알림은 연결된 구성 요소가 종료될 때까지 애플리케이션 처리를 일시 중지하기 위해 메서드를 호출 WaitForExit 하는 데 의존합니다. 비동기 알림은 이벤트에 의존합니다 Exited . 비동기 알림을 EnableRaisingEvents 사용하는 경우 프로세스가 종료되었다는 알림을 받으려면 구성 요소에 대해 Process 설정 true 해야 합니다.