Process.HasExited 속성

정의

연결된 프로세스가 종료되었는지 여부를 나타내는 값을 가져옵니다.

public:
 property bool HasExited { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool HasExited { get; }
[<System.ComponentModel.Browsable(false)>]
member this.HasExited : bool
Public ReadOnly Property HasExited As Boolean

속성 값

구성 요소에서 참조하는 운영 체제 프로세스가 종료되었으면 >이고, 그렇지 않으면 .

특성

예외

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

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

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

예제

다음 예제에서는 메모장 인스턴스를 시작합니다. 그런 다음, 최대 10초 동안 2초 간격으로 연결된 프로세스의 실제 메모리 사용량을 검색합니다. 이 예제에서는 10초가 경과하기 전에 프로세스가 종료되는지 여부를 검색합니다. 이 예제에서는 10초 후에도 계속 실행 중인 경우 프로세스를 닫습니다.

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);
            }
        }
    }
}
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading


try
    use myProcess = Process.Start "Notepad.exe"
    // Display physical memory usage 5 times at intervals of 2 seconds.
    let mutable i = 0

    while i < 5 && not myProcess.HasExited do
        // Discard cached information about the process.
        myProcess.Refresh()
        // Print working set to console.
        printfn $"Physical Memory Usage: {myProcess.WorkingSet64}"
        // Wait 2 seconds.
        Thread.Sleep 2000
        i <- i + 1
    // Close process by sending a close message to its main window.
    myProcess.CloseMainWindow() |> ignore
    // Free resources associated with process.
    myProcess.Close()
with
| :? Win32Exception
| :? FileNotFoundException as e ->
    printfn "The following exception was raised: "
    printfn $"{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 연결된 프로세스가 정상적으로 또는 비정상적으로 종료되었음을 나타냅니다. 호출 CloseMainWindow 하거나 호출하여 연결된 프로세스를 강제로 종료하도록 요청하거나 Kill강제 종료할 수 있습니다. 핸들이 프로세스에 열려 있는 경우 운영 체제는 프로세스가 종료될 때 프로세스 메모리를 해제하지만 핸들, 종료 코드 및 종료 시간과 같은 프로세스에 대한 관리 정보를 유지합니다. 이 정보를 얻으려면 해당 및 ExitTime 속성을 사용할 ExitCode 수 있습니다. 이러한 속성은 이 구성 요소에서 시작한 프로세스에 대해 자동으로 채워집니다. 시스템 프로세스와 연결된 모든 Process 구성 요소가 제거되고 종료된 프로세스에 대한 핸들을 더 이상 보관하지 않을 때 관리 정보가 해제됩니다.

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

Note

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

적용 대상

추가 정보