Process.CloseMainWindow 方法

定義

藉由將關閉訊息傳送至其主視窗,關閉具有使用者介面的程式。

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

傳回

true 如果成功傳送了關閉訊息; false 若相關程序沒有主視窗,或主視窗被停用(例如顯示模態對話框時)。

例外狀況

這個過程已經結束了。

-或-

此物件沒有任何程序相關聯 Process

範例

以下範例啟動一個 Notepad 實例。 接著它會以 2 秒的間隔擷取相關程序的物理記憶體使用量,最多可持續 10 秒。 範例中判斷程序是否在 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 只呼叫本地電腦上正在執行的程序。 你無法讓遠端電腦上的程序退出。 你只能查看遠端電腦上執行的程序資訊。

適用於