Starting a Separate Task


Although thread-based multitasking is what you will use most often when programming in C#, it is possible to utilize process-based multitasking where appropriate. When using process-based multitasking, instead of starting another thread within the same program, one program starts the execution of another program. In C#, you do this by using the Process class. Process is defined within the System.Diagnostics namespace. To conclude this chapter, a brief look at starting and managing another process is offered.

The easiest way to start another process is to use the Start( ) method defined by Process.

Here is one of its simplest forms:

 public static Process Start(string name)

Here, name specifies the name of an executable file that will be executed, or a file that is associated with an executable. It returns a Process object that represents the new process. A Win32Exception is thrown if the specified field cannot be found or cannot be executed.

When a process that you create ends, call Close( ) to free the memory associated with that process. It is shown here:

 public void Close( )

You can terminate a process in two ways. If the process is a Windows GUI application, then to terminate the process, call CloseMainWindow( ), shown here:

 public bool CloseMainWindow( )

This method sends a message to the process, instructing it to stop. It returns true if the message was received. It returns false if the application was not a GUI application, or does not have a main window. Furthermore, CloseMainWindow( ) is only a request to shut down. If the application ignores the request, then the application will not be terminated.

To positively terminate a process, call Kill( ), shown here:

 public void Kill( )

Use Kill( ) carefully. It causes an uncontrolled termination of the process. Any unsaved data associated with the process will most likely be lost.

You can wait for a process to end by calling WaitForExit( ). Its two forms are shown here:

 public void WaitForExit( ) public bool WaitForExit(int milliseconds)

The first form waits until the process terminates. The second waits for only the specified number of milliseconds. The second form returns true if the process has terminated and false if it is still running.

The following program demonstrates how to create, wait for, and close a process. It starts the standard Windows utility program WordPad.exe. It then waits for WordPad to end.

 // Starting a new process. using System; using System.Diagnostics; class StartProcess {   public static void Main() {     Process newProc = Process.Start("wordpad.exe");     Console.WriteLine("New process started.");     newProc.WaitForExit();     newProc.Close(); // free resources     Console.WriteLine("New process ended.");   } }

When you run this program, WordPad will start up, and you will see the message “New process started.” The program will then wait until you close WordPad. Once WordPad has been terminated, the final message “New process ended.” is displayed.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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