How to wait for a shelled application to finish by using Visual C#

Article translations Article translations
Article ID: 305369 - View products that this article applies to.
This article was previously published under Q305369
For a Microsoft Visual Basic .NET version of this article, see 305368.
For a Microsoft Visual C++ .NET version of this article, see 307388.

This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Diagnostics
Expand all | Collapse all

On This Page

Summary

This step-by-step article shows you how to use the .NET Framework Process class to start another application from your code and have the code wait for the other application to close before it continues.

When the code waits for the application to finish, there are two options:
  • Wait indefinitely for the other application to either finish or be closed by the user.
  • Specify a time-out period after which you can close the application from your code.
This article presents two code samples that demonstrate both approaches. In addition, the time-out example allows the possibility that the other application may have stopped responding (hung) and takes the necessary steps to close the application.

Requirements

  • Microsoft Visual C# 2008, Microsoft Visual C# 2005, or Microsoft Visual C# .NET

Include namespaces

You must import the namespace for the Process class before you run the examples that follow. Place the following line of code before the Namespace or Class declaration that contains the code sample:
using System.Diagnostics;
				

Wait indefinitely for the shelled application to finish

The following code sample starts another application (in this case, Notepad) and waits indefinitely for the application to close:
    //How to Wait for a Shelled Process to Finish
    //Get the path to the system folder.
    string sysFolder= 
	Environment.GetFolderPath(Environment.SpecialFolder.System);
    //Create a new process info structure.
    ProcessStartInfo pInfo = new ProcessStartInfo();
    //Set the file name member of the process info structure.
    pInfo.FileName = sysFolder + @"\eula.txt";
    //Start the process.
    Process p = Process.Start(pInfo);
    //Wait for the window to finish loading.
    p.WaitForInputIdle();
    //Wait for the process to end.
    p.WaitForExit();
    MessageBox.Show("Code continuing...");
				

Provide a time-out for the shelled application

The following code sample sets a time-out for the shelled application. The time-out for the example is set to 5 seconds. You may want to adjust this number (which is calculated in milliseconds) for your testing.
    //Set a time-out value.
    int timeOut=5000;
    //Get path to system folder.
    string sysFolder= 
      Environment.GetFolderPath(Environment.SpecialFolder.System);
    //Create a new process info structure.
    ProcessStartInfo pInfo = new ProcessStartInfo();
    //Set file name to open.
    pInfo.FileName = sysFolder + @"\eula.txt";
    //Start the process.
    Process p = Process.Start(pInfo);
    //Wait for window to finish loading.
    p.WaitForInputIdle();
    //Wait for the process to exit or time out.
    p.WaitForExit(timeOut);
    //Check to see if the process is still running.
    if (p.HasExited == false)
    	//Process is still running.
    	//Test to see if the process is hung up.
    	if (p.Responding)
      	    //Process was responding; close the main window.
       	    p.CloseMainWindow();
    	else
     	   //Process was not responding; force the process to close.
	   p.Kill();
    MessageBox.Show("Code continuing...");
				

Troubleshooting

Sometimes it may be difficult to choose between these two options. The primary purpose of a time-out is to prevent your application from hanging because the other application has hung. Time-outs are better suited for a shelled application that performs background processing, in which the user may not know that the other application has stalled or does not have a convenient way to close it.

References

For more information about how to use an asynchronous method to accomplish this task, click the following article number to view the article in the Microsoft Knowledge Base:
304032 How to determine when a shelled process ends in Visual C#

Properties

Article ID: 305369 - Last Review: October 15, 2012 - Revision: 4.0
Applies to
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbdiagnostics kbhowtomaster kbsample KB305369

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com