Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Managing Processes in .NET : Page 3

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
Displaying Process Information
To make sense of the methods and properties in Table 1 and Table 2, let's build a sample applet that displays some of the information you normally get through the Windows Task Manager. Listing 1 shows the code that grabs some information about all the currently active processes and populates a ListView control with names, working set, and start time.

The GetProcesses shared method returns an array of Process objects, one per each active process. The code in Listing 1 walks its way through the array and adds an item to a ListView control with the values of the ProcessName, WorkingSet, and StartTime properties. This information, and in particular the working set, corresponds to that displayed by the Windows Task Manager (see Figure 1).

 
Figure 1: A sample application based on the .NET process API that mimics the Windows Task Manager.
In Figure 1 you also see a second ListView control filled with modules information. An active process consists of various modules—that is, dynamic load libraries that it depends on for some functionality. The Modules property returns a collection of ProcessModule objects, each pointing to a different library or assembly. Listing 2 shows the source code that populates the second ListView control when a process is selected.

Specify Startup Information
When you start a new process, especially if the program is a console program, you need to pass some command line arguments. How do you do that? The Start shared method has an overload that allows you to pass a command line as a string.

Process.Start("notepad.exe", "myfile.txt")

When your application uses a shared overload of the Start method, it always creates a new Process object. If you want to start a new process based on an existing instance of the Process class, you use the instance-based version of the Start method. In this case, though, you can't specify any parameter because the signature of the method doesn't allow it. Any startup information should be specified through the ProcessStartInfo class.

Dim info As New ProcessStartInfo info.Arguments = args info.FileName = exe Dim p As New Process p.StartInfo = info p.Start()

The FileName property lets you indicate the name of the executable to spawn. The Arguments property allows you to set the command line string. However, using the ProcessStartInfo class is a bit overkill if you only need to specify the executable and a command line. There are other reasons to use startup information. Table 3 lists the properties of the ProcessStartInfo class.

Table 3: Properties of the ProcessStartInfo class.



Property

Description

Arguments

Gets or sets the command line to use when starting the application.

CreateNoWindow

Indicates whether to start the process in a new window.

EnvironmentVariables

Returns the collection of all environment variables.

ErrorDialog

Indicates whether an error dialog is displayed to the user if the process cannot be started.

ErrorDialogParentHandle

Gets or sets the window handle to use when an error dialog is shown.

FileName

Indicates the program, or the document, to start.

RedirectStandardError

Indicates whether the error output of the program must be redirected to the stream set in the StandardError member of the Process object.

RedirectStandardInput

Indicates whether the process command input is read from the stream set in the StandardInput member of the Process object.

RedirectStandardOutput

Indicates whether the process output is written to the stream set in the StandardOutput member of the Process object.

UseShellExecute

Indicates whether the program, or document, must be started through the shell using the ShellExecuteEx Win32 function.

Verb

Gets or sets the verb to use when opening the program or document.

Verbs

Returns the list of the verbs associated with the type of file specified by the FileName property.

WindowStyle

Gets or sets the window state to use when the process is started.

WorkingDirectory

Gets or sets the initial directory for the process.


In the case of redirected output, the Process class accumulates the output into a memory stream instead of just sending it out to the console output. You can then access any byte output through the Process class' StandardOutput property.
The ProcessStartInfo class has a lot of interesting properties. I'll focus on a few of them: CreateNoWindow, Verb, and the three redirecting properties (RedirectStandardError, RedirectStandardInput, and RedirectStandardOutput). The CreateNoWindow Boolean property instructs the Process class not to display a window when executing the program. This is good when you need to run a console process in the background and you don't want that ugly black screen of the MS-DOS console to pop up. To make sure that the console window doesn't show up, you should have the CreateProcess API start the process by setting the UseShellExecute property to false.

Dim info As New ProcessStartInfo info.Arguments = args info.FileName = exe info.CreateNoWindow = True info.UseShellExecute = False Dim p As New Process p.StartInfo = info p.Start()

Each file in Windows has a context menu, and each context menu has a set of predefined verbs like Open and Print. Windows defines the verbs based on the type of the file (that is, the extension) in the system registry. In the end, a verb is a string; the Verbs property returns an array of strings filled with the verbs found for the type of the file set to the FileName property. Each file type has a default verb—normally Open. When you start a process through the shell (see UseShellExecute), Windows executes its default verb. As I mentioned, in most cases this verb is Open and either the program runs or the document opens. However, be aware that this is a configurable parameter.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date