How to: Stop Processes
There are two methods you can use to stop a process with a Process component. The method you use depends on the type of process being stopped:
If the process has a graphical user interface, call the CloseMainWindow method. This method sends a close request to the main window of the process and behaves in the same manner as selecting the Close command from the user interface. Using this method gives the target program a chance to prompt the user to save any unsaved data during the cleanup operation.
If the process does not have a user interface, call the Kill method.
Figyelmeztetés
Calling the Kill method will stop the process immediately without prompting to save changed data. Any unsaved data will be lost.
If you want the component to be notified when the operating system has shut down a process, you must set the EnableRaisingEvents property to true. The EnableRaisingEvents property is used in asynchronous processing to notify your application that a process has exited.
Call the GetProcessesByName method to retrieve the process you want to stop.
For more information, see How to: Bind to Existing Processes.
Call one of the following methods:
If the process has a user interface, call the CloseMainWindow method.
If the process is windowless, call the Kill method.
The following example shows how to call the CloseMainWindow method to close all instances of Notepad currently running on a local computer:
Dim myProcesses() As Process Dim myProcess As Process ' Returns array containing all instances of "Notepad". myProcesses = Process.GetProcessesByName("Notepad") For Each myProcess In myProcesses myProcess.CloseMainWindow() Next
Process[] myProcesses; // Returns array containing all instances of Notepad. myProcesses = Process.GetProcessesByName("Notepad"); foreach (Process myProcess in myProcesses) { myProcess.CloseMainWindow(); }
How to: Bind to Existing Processes