Process.CloseMainWindow 方法

定義

關閉有使用者介面的處理序,方法是傳送關閉訊息至其主視窗。Closes a process that has a user interface by sending a close message to its main window.

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

傳回

如果已成功傳送關閉訊息,則為 true;如果相關聯處理序沒有主視窗或主視窗已停用 (例如,如果正在顯示強制回應對話方塊),則為 falsetrue if the close message was successfully sent; false if the associated process does not have a main window or if the main window is disabled (for example if a modal dialog is being shown).

例外狀況

處理程序已經結束。The process has already exited.

-或--or- 沒有和這個 Process 物件相關聯的處理序。No process is associated with this Process object.

範例

下列範例會啟動記事本的實例。The following example starts an instance of Notepad. 然後,它會以2秒的間隔來抓取相關進程的實體記憶體使用量,最多10秒。It then retrieves the physical memory usage of the associated process at 2 second intervals for a maximum of 10 seconds. 此範例會偵測進程是否會在10秒之前結束。The example detects whether the process exits before 10 seconds have elapsed. 此範例會關閉進程(如果它仍然在10秒後執行)。The example closes the process if it is still running after 10 seconds.

#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

備註

當處理常式正在執行時,其訊息迴圈會處於等候狀態。When a process is executing, its message loop is in a wait state. 每次由作業系統將 Windows 訊息傳送至進程時,就會執行訊息迴圈。The message loop executes every time a Windows message is sent to the process by the operating system. 呼叫 CloseMainWindow 會傳送關閉主視窗的要求,在格式正確的應用程式中,關閉子視窗,並撤銷應用程式所有正在執行的訊息迴圈。Calling CloseMainWindow sends a request to close the main window, which, in a well-formed application, closes child windows and revokes all running message loops for the application. 藉由呼叫 CloseMainWindow 來結束進程的要求不會強制應用程式結束。The request to exit the process by calling CloseMainWindow does not force the application to quit. 應用程式可以在結束之前要求使用者驗證,否則可能會拒絕結束。The application can ask for user verification before quitting, or it can refuse to quit. 若要強制關閉應用程式,請使用 Kill 方法。To force the application to quit, use the Kill method. CloseMainWindow 的行為等同于使用者使用 [系統] 功能表來關閉應用程式的主視窗。The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. 因此,關閉主視窗來結束進程的要求並不會強制應用程式立即終止。Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.

如果您呼叫 Kill,進程所編輯的資料或配置給進程的資源可能會遺失。Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill 會導致異常的進程終止,而且只在必要時才使用。Kill causes an abnormal process termination, and should be used only when necessary. CloseMainWindow 可依序終止進程並關閉所有視窗,因此最好是具有介面的應用程式。CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface. 如果 CloseMainWindow 失敗,您可以使用 Kill 來終止進程。If CloseMainWindow fails, you can use Kill to terminate the process. Kill 是終止沒有圖形化介面之進程的唯一方法。Kill is the only way to terminate processes that do not have graphical interfaces.

您只能針對在本機電腦上執行的進程呼叫 KillCloseMainWindowYou can call Kill and CloseMainWindow only for processes that are running on the local computer. 您無法讓遠端電腦上的進程結束。You cannot cause processes on remote computers to exit. 您只能查看遠端電腦上執行之進程的資訊。You can only view information for processes running on remote computers.

安全性

LinkDemand
針對立即呼叫者的完全信任的。for full trust for the immediate caller. 這個成員無法供部分信任的程式碼使用。This member cannot be used by partially trusted code.

適用於