Process.CloseMainWindow 方法

定義

關閉有使用者介面的處理序,方法是傳送關閉訊息至其主視窗。

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

傳回

Boolean

如果已成功傳送關閉訊息,則為 true;如果相關聯處理序沒有主視窗或主視窗已停用 (例如,如果正在顯示強制回應對話方塊),則為 false

例外狀況

處理程序已經結束。

-或-

沒有和這個 Process 物件相關聯的處理序。

範例

下列範例會啟動記事本的實例。 然後,它會以最多 10 秒的間隔擷取相關聯進程的實體記憶體使用量。 此範例會偵測進程是否在 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 是終止沒有圖形化介面之進程的唯一方式。

您只能針對在本機電腦上執行的進程呼叫 KillCloseMainWindow 。 您無法讓遠端電腦上的進程結束。 您只能檢視遠端電腦上執行之進程的資訊。

適用於