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

속성 값

Process 구성 요소가 참조하는 운영 체제 프로세스가 종료되었으면 true이고, 그러지 않으면 false입니다.

특성

예외

개체와 연결된 프로세스가 없습니다.

프로세스의 종료 코드를 검색할 수 없습니다.

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

예제

다음 예제에서는 메모장의 instance 시작합니다. 그런 다음, 최대 10초 동안 2초 간격으로 연결된 프로세스의 실제 메모리 사용량을 검색합니다. 이 예제에서는 프로세스가 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

설명

trueHasExited 값은 연결된 프로세스가 정상적으로 또는 비정상적으로 종료되었음을 나타냅니다. 또는 Kill를 호출 CloseMainWindow 하여 연결된 프로세스를 요청하거나 강제로 종료할 수 있습니다. 핸들이 프로세스에 열려 있는 경우 운영 체제는 프로세스가 종료될 때 프로세스 메모리를 해제하지만 핸들, 종료 코드 및 종료 시간과 같은 프로세스에 대한 관리 정보를 유지합니다. 이 정보를 얻으려면 및 ExitTime 속성을 사용할 ExitCode 수 있습니다. 이러한 속성은 이 구성 요소에서 시작한 프로세스에 대해 자동으로 채워집니다. 시스템 프로세스와 연결된 모든 Process 구성 요소가 제거되고 종료된 프로세스에 대한 핸들을 더 이상 보유하지 않을 때 관리 정보가 해제됩니다.

프로세스는 코드와 독립적으로 종료할 수 있습니다. 이 구성 요소를 사용하여 프로세스를 시작한 경우 연결된 프로세스가 독립적으로 종료되더라도 시스템은 의 값을 HasExited 자동으로 업데이트합니다.

참고

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

적용 대상

추가 정보