4.11 Terminate a Process


Problem

You need to terminate a process such as an application or a service.

Solution

Obtain a Process object representing the operating system process you want to terminate. For Windows-based applications, call Process.CloseMainWindow to send a close message to the application's main window. For Windows-based applications that ignore CloseMainWindow , or for non-Windows-based applications, call the Process.Kill method.

Discussion

If you start a new process from managed code using the Process class (discussed in recipe 4.10), you can terminate the process using the Process object that represents the new process. You can also obtain Process objects that refer to other currently running processes using the static methods of the Process class summarized in Table 4.4.

Table 4.4: Methods for Obtaining Process References

Method

Description

GetCurrentProcess

Returns a Process object representing the currently active process.

GetProcessById

Returns a Process object representing the process with the specified ID.

GetProcesses

Returns an array of Process objects representing all currently active processes.

GetProcessesByName

Returns an array of Process objects representing all currently active processes with a specified friendly name. The friendly name is the name of the executable excluding file extension or path ; for example, notepad or calc .

Once you have a Process object representing the process that you want to terminate, you need to call either the CloseMainWindow method or the Kill method. The CloseMainWindow method sends a close message to a Windows- based application's main window. This method has the same effect as if the user had closed the main window using the system menu, and it gives the application the opportunity to perform its normal shutdown routine. CloseMainWindow won't terminate applications that do not have a main window or applications with a disabled main window ”possibly because a modal dialog box is currently displayed. Under such circumstances, CloseMainWindow will return false .

CloseMainWindow returns true if the close message was successfully sent, but doesn't guarantee that the process is actually terminated . For example, applications used to edit data will usually give the user the opportunity to save unsaved data if a close message is received. The user usually has the chance to cancel the close operation under such circumstances. This means that CloseMainWindow will return true , but the application will still be running once the user cancels. You can use the Process.WaitForExit method to signal process termination and the Process.HasExited property to test if a process has terminated. Alternatively, you can use the Kill method.

The Kill method simply terminates a process immediately; the user has no chance to stop the termination, and all unsaved data is lost. Kill is the only option for terminating Windows-based applications that do not respond to CloseMainWindow and for terminating non-Windows-based applications.

The following example starts a new instance of Notepad, waits five seconds, and then terminates the Notepad process. The example first tries to terminate the process using CloseMainWindow . If CloseMainWindow returns false , or the Notepad process is still running after CloseMainWindow is called, the example calls Kill and forces the Notepad process to terminate; you can force CloseMainWindow to return false by leaving the File Open dialog box open .

 using System; using System.Threading; using System.Diagnostics; public class TerminateProcessExample {     public static void Main () {         // Create a new Process and run notepad.exe.         using (Process process = Process.Start("notepad.exe")) {             // Wait for 5 seconds and terminate the notepad process.             Console.WriteLine("Waiting 5 seconds before terminating" +                 " notepad.exe.");             Thread.Sleep(5000);             // Terminate notepad process.             Console.WriteLine("Terminating Notepad with CloseMainWindow.");             // Try to send a close message to the main window.             if (!process.CloseMainWindow()) {                 // Close message did not get sent - Kill Notepad.                 Console.WriteLine("CloseMainWindow returned false - " +                     " terminating Notepad with Kill.");                 process.Kill();             } else {                 // Close message sent successfully; wait for 2 seconds                 // for termination confirmation before resorting to Kill.                 if (!process.WaitForExit(2000)) {                     Console.WriteLine("CloseMainWindow failed to" +                         " terminate - terminating Notepad with Kill.");                     process.Kill();                 }             }         }         // Wait to continue.         Console.WriteLine("Main method complete. Press Enter.");         Console.ReadLine();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net