Process.Kill 方法

定義

多載

Kill()

立即停止相關的處理序。Immediately stops the associated process.

Kill(Boolean)

立即停止關聯的處理序,並選擇性地停止其子處理序。Immediately stops the associated process, and optionally its child/descendent processes.

備註

Kill 方法會強制終止進程,而 CloseMainWindow 只會要求終止。The Kill method forces a termination of the process, while CloseMainWindow only requests a termination. 當具有圖形化介面的進程正在執行時,其訊息迴圈會處於等候狀態。When a process with a graphical interface 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 方法會以非同步方式執行。The Kill method executes asynchronously. 呼叫 Kill 方法之後,請呼叫 WaitForExit 方法來等候進程結束,或檢查 HasExited 屬性以判斷進程是否已結束。After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.

注意

WaitForExit 方法和 HasExited 屬性不會反映子系進程的狀態。The WaitForExit method and the HasExited property do not reflect the status of descendant processes. 當使用 Kill(entireProcessTree: true) 時,WaitForExitHasExited 會指出在指定的進程結束之後,即使所有子系尚未結束,也會結束。When Kill(entireProcessTree: true) is used, WaitForExit and HasExited will indicate that exiting has completed after the given process exits, even if all descendants have not yet exited.

如果您呼叫 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.

注意

如果在進程目前正在終止時呼叫 Kill 方法,則會擲回 Win32Exception 以拒絕存取。If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied.

Kill()

立即停止相關的處理序。Immediately stops the associated process.

public:
 void Kill();
public void Kill ();
member this.Kill : unit -> unit
Public Sub Kill ()

例外狀況

相關的處理序無法終止。The associated process could not be terminated.

-或--or-

正在結束處理序。The process is terminating.

您正在嘗試為遠端電腦上執行的處理序呼叫 Kill()You are attempting to call Kill() for a process that is running on a remote computer. 這個方法僅供在本機電腦執行的處理序使用。The method is available only for processes running on the local computer.

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

-或--or- 沒有任何與這個 Process 物件關聯的處理序。There is no process associated with this Process object.

安全性

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

另請參閱

Kill(Boolean)

立即停止關聯的處理序,並選擇性地停止其子處理序。Immediately stops the associated process, and optionally its child/descendent processes.

public:
 void Kill(bool entireProcessTree);
public void Kill (bool entireProcessTree);
member this.Kill : bool -> unit
Public Sub Kill (entireProcessTree As Boolean)

參數

entireProcessTree
Boolean

true 表示終止關聯的處理序及其子系;false 表示僅終止關聯的處理序。true to kill the associated process and its descendants; false to kill only the associated process.

例外狀況

相關的處理序無法終止。The associated process could not be terminated.

-或--or-

正在結束處理序。The process is terminating.

您正在嘗試為遠端電腦上執行的處理序呼叫 Kill()You are attempting to call Kill() for a process that is running on a remote computer. 這個方法僅供在本機電腦執行的處理序使用。The method is available only for processes running on the local computer.

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

-或--or- 沒有任何與這個 Process 物件關聯的處理序。There is no process associated with this Process object.

-或--or-

呼叫處理序是關聯處理序的子樹狀目錄成員。The calling process is a member of the associated process' descendant tree.

關聯處理序的子樹狀目錄中並非所有處理序都可終止。Not all processes in the associated process' descendant tree could be terminated.

備註

entireProcessTree 設定為 true時,子系終止程式會無訊息地略過呼叫無權查看詳細資料的進程,因為終止進程無法判斷這些進程是否為子系。When entireProcessTree is set to true, processes where the call lacks permissions to view details are silently skipped by the descendant termination process because the termination process is unable to determine whether those processes are descendants.

安全性

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

另請參閱

適用於