Bewerken

Share via


Process.Kill Method

Definition

Forces termination of the underlying process.

Overloads

Kill()

Immediately stops the associated process.

Kill(Boolean)

Immediately stops the associated process, and optionally its child/descendent processes.

Remarks

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. The message loop executes every time a Windows message is sent to the process by the operating system. 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. 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. To force the application to quit, use the Kill method.

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.

Note

The Kill method executes asynchronously. 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.

Note

The WaitForExit method and the HasExited property do not reflect the status of descendant processes. 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.

Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill causes an abnormal process termination and should be used only when necessary. CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface. If CloseMainWindow fails, you can use Kill to terminate the process. Kill is the only way to terminate processes that do not have graphical interfaces.

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()

Source:
Process.Unix.cs
Source:
Process.Unix.cs
Source:
Process.Unix.cs

Immediately stops the associated process.

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 ()
Attributes

Exceptions

The associated process could not be terminated.

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.

There is no process associated with this Process object.

See also

Applies to

Kill(Boolean)

Source:
Process.NonUap.cs
Source:
Process.NonUap.cs
Source:
Process.NonUap.cs

Immediately stops the associated process, and optionally its child/descendent processes.

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)

Parameters

entireProcessTree
Boolean

true to kill the associated process and its descendants; false to kill only the associated process.

Attributes

Exceptions

The associated process could not be terminated.

-or-

The process is terminating.

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.

.NET Framework and .NET Core 3.0 and earlier versions only: The process has already exited.

-or-

There is no process associated with this Process object.

-or-

The calling process is a member of the associated process's descendant tree.

Not all processes in the associated process's descendant tree could be terminated.

Remarks

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.

See also

Applies to