Process.ExitCode 속성

정의

연결된 프로세스가 종료될 때 연결된 프로세스에서 지정한 값을 가져옵니다.

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

속성 값

연결된 프로세스가 종료될 때 연결된 프로세스에서 지정한 코드입니다.

특성

예외

프로세스가 끝나지 않았습니다.

또는

프로세스 Handle이 유효하지 않습니다.

원격 컴퓨터에서 실행 중인 프로세스에 대한 ExitCode 속성에 액세스하려고 합니다. 이 속성은 로컬 컴퓨터에서 실행되는 프로세스에만 사용할 수 있습니다.

예제

다음 예제에서는 메모장의 instance 시작합니다. 그런 다음, 이 예제는 연결된 프로세스의 다양한 속성을 검색하고 표시합니다. 이 예제에서는 프로세스가 종료되는 시기를 감지하고 프로세스의 종료 코드를 표시합니다.

#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 시스템 프로세스가 종료될 때 반환된 상태 가져옵니다. 프로시저의 정수 반환 값 main() 과 마찬가지로 종료 코드를 사용할 수 있습니다.

ExitCode 해당 프로세스에 대 한 애플리케이션 개발자가 구현한 특정 규칙을 반영 하는 프로세스에 대 한 값입니다. 종료 코드 값을 사용 하 여 코드에서 결정을 내릴 수 하는 경우 애플리케이션 프로세스에서 사용 하는 종료 코드 규칙을 알고 있다고 해야 합니다.

개발자는 일반적으로 값이 0으로 ExitCode 성공적으로 종료되었음을 나타내고 호출 메서드가 비정상적인 프로세스 종료의 원인을 식별하는 데 사용할 수 있는 0이 아닌 값으로 오류를 지정합니다. 이러한 지침을 따를 필요는 없지만 규칙입니다.

프로세스가 종료되기 전에 를 가져오기 ExitCode 위해 시도하면 예외가 throw됩니다. HasExited 먼저 속성을 검사하여 연결된 프로세스가 종료되었는지 확인합니다.

참고

표준 출력이 비동기 이벤트 처리기로 리디렉션된 경우 를 반환true할 때 HasExited 출력 처리가 완료되지 않을 수 있습니다. 비동기 이벤트 처리가 완료되었는지 확인하려면 를 확인하기 전에 매개 변수를 사용하지 않는 오버로드를 호출 WaitForExit() 합니다 HasExited.

또는 메서드를 CloseMainWindowKill 사용하여 연결된 프로세스가 종료되도록 할 수 있습니다.

연결된 프로세스가 종료될 때 알림을 받는 방법에는 동기 및 비동기식이라는 두 가지 방법이 있습니다. 동기 알림 호출에 의존 합니다 WaitForExit 연결된 구성 요소가 종료 될 때까지 애플리케이션의 처리를 일시 중지 하는 방법입니다. 비동기 알림은 이벤트를 사용합니다 Exited . 비동기 알림을 EnableRaisingEvents 사용하는 경우 프로세스가 종료되었다는 알림을 받으려면 구성 요소에 Process 대해 를 로 설정 true 해야 합니다.

적용 대상

추가 정보