共用方式為


Process.Kill 方法

定義

強制終止底層程序。

多載

名稱 Description
Kill()

會立即停止相關流程。

Kill(Boolean)

立即停止相關程序,並可選擇性地停止其子程序或後代程序。

備註

Kill 方法強制終止程序,僅 CloseMainWindow 請求終止。 當具有圖形介面的程序執行時,其訊息迴圈處於等待狀態。 每當作業系統向程序發送 Windows 訊息時,訊息迴圈都會執行。 呼叫 CloseMainWindow 會發送關閉主視窗的請求,在良好形式的應用程式中,這會關閉子視窗並撤銷應用程式所有正在執行的訊息迴圈。 呼叫 CloseMainWindow 退出程序的請求不會強制應用程式退出。 應用程式可以在退出前要求使用者驗證,或是拒絕退出。 要強制應用程式停止,請使用這個 Kill 方法。

CloseMainWindow 行為與使用者使用系統選單關閉應用程式主視窗相同。 因此,關閉主視窗以結束程序的請求,並不會強制應用程式立即退出。

備註

Kill 方法以非同步方式執行。 呼叫 Kill 該方法後,呼叫該 WaitForExit 方法等待程序退出,或檢查 HasExited 屬性以判斷程序是否已退出。

備註

WaitForExit 方法與 HasExited 屬性不反映後繼程序的狀態。 當 Kill(entireProcessTree: true) 使用 時, WaitForExitHasExited 表示在該程序退出後,即使所有後代尚未退出,也表示退出已完成。

程序編輯的資料或分配給該程序的資源,若呼叫 Kill,可能會遺失。 Kill 會導致異常的製程終止,應僅在必要時使用。 CloseMainWindow 能有序終止程序並關閉所有視窗,因此對於有介面的應用程式來說更為理想。 如果 CloseMainWindow 失敗,你可以用 Kill 來終止這個程序。 Kill 是終止沒有圖形介面程序的唯一方式。

你可以呼叫 KillCloseMainWindow 只呼叫本地電腦上正在執行的程序。 你無法讓遠端電腦上的程序退出。 你只能查看遠端電腦上執行的程序資訊。

Kill()

來源:
Process.Unix.cs
來源:
Process.Unix.cs
來源:
Process.Unix.cs
來源:
Process.Unix.cs
來源:
Process.Unix.cs

會立即停止相關流程。

public:
 void Kill();
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public void Kill();
public void Kill();
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public void Kill();
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
member this.Kill : unit -> unit
member this.Kill : unit -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
member this.Kill : unit -> unit
Public Sub Kill ()
屬性

例外狀況

相關程序無法終止。

你正在嘗試呼叫 Kill() 一個在遠端電腦上執行的程序。 此方法僅適用於本地電腦上執行的程序。

這個 Process 物件沒有相關聯的程序。

另請參閱

適用於

Kill(Boolean)

來源:
Process.NonUap.cs
來源:
Process.NonUap.cs
來源:
Process.NonUap.cs
來源:
Process.NonUap.cs
來源:
Process.NonUap.cs

立即停止相關程序,並可選擇性地停止其子程序或後代程序。

public:
 void Kill(bool entireProcessTree);
[System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public void Kill(bool entireProcessTree);
public void Kill(bool entireProcessTree);
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public void Kill(bool entireProcessTree);
[<System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
member this.Kill : bool -> unit
member this.Kill : bool -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
member this.Kill : bool -> unit
Public Sub Kill (entireProcessTree As Boolean)

參數

entireProcessTree
Boolean

true 殺死相關過程及其後代; false 只殺死相關的過程。

屬性

例外狀況

相關程序無法終止。

-或-

程序正在終止。

你正在嘗試呼叫 Kill() 一個在遠端電腦上執行的程序。 此方法僅適用於本地電腦上執行的程序。

僅限 .NET Framework 及 .NET Core 3.0 及更早版本:程序已經退出。

-或-

這個 Process 物件沒有相關聯的程序。

-或-

呼叫程序是相關程序後代樹的成員。

並非所有相關程序後代樹中的程序都能被終止。

備註

entireProcessTree 設定為 true時,呼叫缺乏查看細節權限的程序會被後代終止程序靜默跳過,因為終止程序無法判斷這些程序是否為後代程序。

另請參閱

適用於