advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Debug Mode and Debug Privilege
Disabling the Default Compressed Folder in Windows XP and Windows Server 2003
Getting the WinZip Command Line Tools
Partners & Affiliates
advertisement
CoDe Magazine
Subscribe to CoDe Magazine
Average Rating: 4/5 | Rate this item | 4 users have rated this item.
 Print Print
 
Managing Processes in .NET
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.  

advertisement
ave 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.

Fortunately, the .NET 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).

The Process class provides access to both 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.


Table 1: Methods exposed by the Process class.

Method

Description

Close

Closes the process and frees all the resources associated with the instance.

CloseMainWindow

Sends a WM_CLOSE message to the main window of the process. Makes sense only if the process has a user interface.

EnterDebugMode

Shared method, enables the SeDebugPrivilege privilege on the current thread, meaning that the current process can operate as a debugger.

GetCurrentProcess

Shared method, returns a new instance of the Process class associated with the currently active process.

GetProcessById

Shared method, returns a new instance of the Process class associated with the process matching the specified ID and machine name.

GetProcesses

Shared method, returns an array of Process objects each associated with a currently active process.

GetProcessesByName

Shared method, returns an array of Process objects each associated with a currently active process with the specified name (for example, all instances of Notepad).

Kill

Immediately terminates the associated process.

LeaveDebugMode

Shared method, revokes the privilege that enables the current process to operate as a debugger.

Refresh

Discards any cached information about the process and resets the associated Process object.

Start

Shared method, starts a process and returns the associated Process object. This method also has an instance-based version.

WaitForExit

Stops the current process until the associated process exits or for the specified duration.

WaitForInputIdle

Causes the current process to wait for the associated process to enter an idle state.



  Next Page: Terminating a Process
Page 1: IntroductionPage 4: Printing from the Shell
Page 2: Terminating a ProcessPage 5: Wrapping Zip Functionalities
Page 3: Displaying Process Information 
© Copyright Component Developer Magazine and EPS Software Corp., 2006
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES