Process.CloseMainWindow 메서드

정의

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

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

반환

닫기 메시지를 보낸 경우에는 true이고 연결된 프로세스에 주 창이 없거나 모달 대화 상자가 표시된 때 같이 주 창을 사용할 수 없는 경우에는 false입니다.

예외

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

또는

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

예제

다음 예제에서는 메모장의 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

설명

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

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

로컬 컴퓨터에서 실행되는 프로세스에 대해서만 및 CloseMainWindow 를 호출 Kill 할 수 있습니다. 원격 컴퓨터의 프로세스를 종료할 수 없습니다. 원격 컴퓨터에서 실행되는 프로세스에 대한 정보만 볼 수 있습니다.

적용 대상