Managing Processes in .NET The Process class allows you to gain full control over system processes.You can start and stop processes and retrieve information about running processes such as the list of loaded modules and the characteristics of the memory occupied. The class also features handy methods to know whether a process is responding or has just exited and with which return code. Programmers also have full control over the style of the window the process runs in. After an overview of the capabilities of the Process class, this article demonstrates how to hide running console processes, monitor their execution, and capture any output. I'll use this strategy to create a sample Compression class to use with WinZip and gzip (popular tools for compressing data). Have you ever wanted to manage processes in Win32 code? When it comes to this, there's good and bad news. On the good news side, you can do virtually everything you desire, including block your current process waiting for another process to terminate. On the bad news side, you must work with an API that is not one for the faint-hearted. CreateProcess is the one-size-fits-all function that creates a new Win32 process and manages impersonation and security settings. This function has quite a quirky syntax. The Win32 SDK supplies several functions including CreateProcess and OpenProcess that provide the core functionalities. The Win32 SDK provides ToolHelp API, a secondary SDK, but it is not available on all possible combinations of Win32 operating systems. ToolHelp API offers more advanced functions including the capability to list modules and dependencies, capture the memory footprint, and ping process. | " | The Process class provides access to local and remote processes and enables you to start and stop processes on the local machine. The class represents a superset of the capabilities of the CreateProcess Win32 API.
| " |
Nicely enough, .NET groups all these platform services under a single class?the Process class. Whatever you need to do that involves spawning a new process or controlling a running one, you can do from this unique (and rather powerful) entry point. The Process Class The Process class provides access to local and remote processes and enables you to start and stop processes on the local machine. The class represents a superset of the capabilities of the CreateProcess Win32 API. In addition, the Process class makes working with running instances of applications particularly easy. For example, you only need a single line of code to start a new process from within a .NET application: Process.Start("Notepad.exe")
If you need to open a particular document, say, a Word document, it's even easier. Process.Start("invoice.doc")
In this case, the Process class is smart enough to figure out that the argument is not an executable. It looks up the system registry for a match on the extension (.doc in this case), gets the executable associated with that type of file, and starts it up. This behavior reveals another powerful Win32 API function working under the hood?ShellExecuteEx, which is designed to open a document?be it an executable (that is, an .exe or .bat file), or a document file associated with a program (such as a .txt or a .doc file). The Process class has both instance and shared (static, if you speak C#) methods. Table 1 lists all the methods exposed by the class. Looking at the table will give you a clear idea of the class' capabilities. You have two ways to terminate a process?Kill and CloseMainWindow. The former stops execution immediately, whereas the latter simply posts a termination message to the message queue. The Kill method can cause loss of data if it reaches the process at a critical stage. You must use the Kill method if you have to programmatically terminate a program that doesn't have a graphical user interface (such as a console program or a system service). You should always use CloseMainWindow to stop GUI programs. You may have a programming situation in which you need to spawn an external program and wait for it to terminate. By default, a spawned process runs independently from the parent. To synchronize the two so that the parent resumes when the child has completed, you use the WaitForExit method. WaitForExit is an instance-based method that requires a fresh instance of the Process class to work. Dim p As New Process p = Process.Start("notepad.exe") p.WaitForExit()
The Start method has several overloads; some shared and one instance-based. The instance-based overload doesn't accept parameters. To specify the name of the executable to run, you need to instantiate and fill a class named ProcessStartInfo. Let's discover more about this class and process information in general. | & | | 
By: Leonardo Esposito
dinoesp@hotmail.com
Dino Esposito is a mentor at Solid Quality Mentors where he manages the ASP.NET, workflow, and AJAX courseware. A speaker at many industry events including Microsoft TechEd, Basta, DevWeek, and DevConnections, Dino is the author of the two volumes of Programming Microsoft ASP.NET 2.0 Applications, for Microsoft Press. You can find late breaking news at http://weblogs.asp.net/despos.
leesposi@libero.it | Fast Facts | | If you need to launch programs from within a .NET application, the Process class is the right tool. It not only allows you to start and stop a process, but it also provides detailed information about running processes. Using the Process class you can easily access the modules and memory footprint of each process so that you can emulate the Windows Tasks Manager. Furthermore, the process startup information you're allowed to specify provides fertile ground so you can implement advanced functions such as capturing the output of a console program and wrapping a console program into a set of .NET classes. | |
|