Stops the associated process immediately.
[Visual Basic] Public Sub Kill() [C#] public void Kill(); [C++] public: void Kill(); [JScript] public function Kill();
Exception Type | Condition |
---|---|
Win32Exception | The associated process could not be terminated for the reason specified in the string. For example, the [give example] or [give another example]. |
SystemException | No process Id has been set, and a Handle from which the Id can be determined does not exist.
-or- There is no process associated with this Process object. -or- You are attempting to call Kill on a process running on a remote computer. The method is only available for processes running on the local computer. |
Kill forces a shutdown, while CloseMainWindow only requests a shutdown. When a process with a graphical interface is executing, its message loop is in a wait state, and executes every time a windows message is sent to the process by the operating system. Calling CloseMainWindow revokes all running message loops on all threads. The request to exit the process by calling CloseMainWindow does not force the application to quit. It asks for user verification to perform the shutdown, or it refuses shutdown if the application is running a macro.
Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill causes an abnormal process termination, so only use it when necessary. CloseMainWindow enables an orderly shutdown of the process and closes all windows, so it is preferable for applications with an interface. If CloseMainWindow fails, you might have to call Kill to terminate the process. Kill is the only way to terminate processes that do not have graphical interfaces, because these processes do not have a message loop that can receive and handle a windows Close message.
Note Currently, all processes started using a Process component open in a command window, so they contain a message loop and you can attempt to close them using CloseMainWindow. This will change before final release. Depending on how the non-graphical interface process is coded, CloseMainWindow may not succeed and Kill may be necessary in order to stop the process.
You can only call Kill and CloseMainWindow on processes that are running on the local computer. For security reasons, you cannot cause processes on remote computers to exit. You can only view information for processes running on remote computers.
Process Class | Process Members | System.Diagnostics Namespace | CloseMainWindow | Start