Process.Kill 方法

定義

強制終止基礎處理序。

多載

Kill()

立即停止相關的處理序。

Kill(Boolean)

立即停止關聯的處理序,並選擇性地停止其子處理序。

備註

方法 Kill 會強制終止進程,而 CloseMainWindow 只要求終止。 當具有圖形化介面的進程正在執行時,其訊息循環處於等候狀態。 每當 Windows 訊息傳送至作業系統的進程時,訊息迴圈就會執行。 呼叫 CloseMainWindow 會傳送要求以關閉主視窗,在格式正確的應用程式中關閉子視窗,並撤銷應用程式的所有執行中訊息迴圈。 呼叫 以結束進程 CloseMainWindow 的要求不會強制應用程式結束。 應用程式可以在結束之前要求用戶驗證,也可以拒絕結束。 若要強制應用程式結束,請使用 Kill 方法。

的行為與使用系統功能表關閉應用程式主視窗的用戶行為 CloseMainWindow 相同。 因此,關閉主視窗以結束進程的要求不會強制應用程式立即結束。

注意

方法會 Kill 以異步方式執行。 呼叫 Kill 方法之後,請呼叫 WaitForExit 方法來等候進程結束,或檢查 HasExited 屬性以判斷進程是否已結束。

注意

方法和 WaitForExitHasExited 屬性不會反映子系進程的狀態。 使用 時 Kill(entireProcessTree: true)WaitForExitHasExited 會指出在指定的進程結束之後結束,即使所有子系尚未結束也一樣。

如果您呼叫 Kill,則由配置給進程的進程或資源所編輯的數據可能會遺失。 Kill 會導致異常的進程終止,而且只在必要時才應該使用。 CloseMainWindow 會啟用程式的順序終止,並關閉所有視窗,因此最好是具有 介面的應用程式。 如果 CloseMainWindow 失敗,您可以使用 Kill 來終止進程。 Kill 是終止沒有圖形化介面之進程的唯一方式。

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

Kill()

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

立即停止相關的處理序。

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

例外狀況

相關的處理序無法終止。

您正在嘗試為遠端電腦上執行的處理序呼叫 Kill() 。 這個方法僅供在本機電腦執行的處理序使用。

沒有任何與這個 Process 物件關聯的處理序。

另請參閱

適用於

Kill(Boolean)

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

立即停止關聯的處理序,並選擇性地停止其子處理序。

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

參數

entireProcessTree
Boolean

true 表示終止關聯的處理序及其子系;false 表示僅終止關聯的處理序。

屬性

例外狀況

相關的處理序無法終止。

-或-

正在結束處理序。

您正在嘗試為遠端電腦上執行的處理序呼叫 Kill() 。 這個方法僅供在本機電腦執行的處理序使用。

僅限 .NET Framework 和 .NET Core 3.0 和舊版:程式已結束。

-或-

沒有任何與這個 Process 物件關聯的處理序。

-或-

呼叫進程是相關聯進程子系樹狀結構的成員。

並非所有關聯進程子系樹狀結構中的所有進程都可以終止。

備註

當 設定為 trueentireProcessTree,呼叫缺少檢視詳細數據的許可權的進程會由子系終止進程以無訊息方式略過,因為終止程式無法判斷這些進程是否為子系。

另請參閱

適用於