Process.CloseMainWindow 메서드

정의

주 창에 닫기 메시지를 보내 사용자 인터페이스가 있는 프로세스를 닫습니다.

public:
 bool CloseMainWindow();
public bool CloseMainWindow();
member this.CloseMainWindow : unit -> bool
Public Function CloseMainWindow () As Boolean

반품

닫기 메시지가 성공적으로 전송되었으면 이고, 연결된 프로세스에 주 창이 없거나 주 창이 비활성화되어 있으면(예: 모달 대화 상자가 표시되는 경우)

예외

프로세스가 이미 종료되었습니다.

-또는-

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

예제

다음 예제에서는 메모장 인스턴스를 시작합니다. 그런 다음, 최대 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

설명

프로세스가 실행 중이면 메시지 루프가 대기 상태입니다. 메시지 루프는 운영 체제에서 프로세스에 Windows 메시지를 보낼 때마다 실행됩니다. 호출 CloseMainWindow 은 기본 창을 닫는 요청을 보냅니다. 이 창은 올바른 형식의 애플리케이션에서 자식 창을 닫고 애플리케이션에 대해 실행 중인 모든 메시지 루프를 취소합니다. 호출 CloseMainWindow 을 통해 프로세스를 종료하라는 요청이 애플리케이션을 강제로 종료하지는 않습니다. 애플리케이션은 종료하기 전에 사용자 확인을 요청하거나 종료를 거부할 수 있습니다. 애플리케이션을 강제로 종료하려면 이 메서드를 Kill 사용합니다. 동작은 시스템 메뉴를 사용하여 애플리케이션의 주 창을 닫는 사용자의 동작 CloseMainWindow 과 동일합니다. 따라서 주 창을 닫아 프로세스를 종료하라는 요청이 애플리케이션을 즉시 종료하도록 강제하지는 않습니다.

호출 Kill하는 경우 프로세스 또는 프로세스에 할당된 리소스에서 편집한 데이터가 손실될 수 있습니다. Kill 는 비정상적인 프로세스 종료를 발생시키고 필요한 경우에만 사용해야 합니다. CloseMainWindow 는 프로세스를 순서대로 종료하고 모든 창을 닫기 때문에 인터페이스가 있는 애플리케이션에 적합합니다. 실패하면 CloseMainWindow 프로세스를 종료하는 데 사용할 Kill 수 있습니다. Kill 는 그래픽 인터페이스가 없는 프로세스를 종료하는 유일한 방법입니다.

로컬 컴퓨터에서 실행 중인 프로세스에 대해서만 호출 KillCloseMainWindow 할 수 있습니다. 원격 컴퓨터의 프로세스가 종료되도록 할 수 없습니다. 원격 컴퓨터에서 실행되는 프로세스에 대한 정보만 볼 수 있습니다.

적용 대상