Process.Kill 方法

定义

强制终止基础进程。Forces termination of the underlying process.

重载

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)使用时, WaitForExit HasExited 将指示在给定进程退出后退出已完成,即使所有子代尚未退出也是如此。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.

如果调用,则由进程编辑的数据或分配给进程的资源可能会丢失 KillData 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.

只能为在 Kill CloseMainWindow 本地计算机上运行的进程调用和。You 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()

立即停止关联的进程。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.

你正尝试为远程计算机上运行的进程调用 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.

没有与此 Process 对象关联的进程。There is no process associated with this Process object.

另请参阅

适用于

产品 Introduced

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.

另请参阅

适用于

产品 Introduced