Microsoft KB Archive/307388

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

Article ID: 307388

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-



This article was previously published under Q307388



For a Microsoft Visual Basic .NET version of this article, see 305368.

For a Microsoft C# .NET version of this article, see 305369.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Diagnostics
 * System.Windows.Forms

IN THIS TASK
SUMMARY Include DLLs Include Namespaces
 * Option 1: Wait Indefinitely for the Shelled Application to Finish
 * Option 2: Provide a Time-out for the Shelled Application
 * Choosing Between the Two Options

REFERENCES



SUMMARY
This article demonstrates 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. -or-


 * 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 can recognize if the other application has stopped responding and will take the necessary steps to close the application.

back to the top

Include DLLs
You must import the DLLs for both the Process and the MessageBox classes before you run the examples that follow. Place the following lines of code right after the #include lines: back to the top
 * 1) using 
 * 2) using 

Include Namespaces
You must also import the namespaces for both the Process and the MessageBox classes. Place the following lines of code after the #using statements: using namespace System::Diagnostics; using namespace System::Windows::Forms; back to the top

Option 1: Wait Indefinitely for the Shelled Application to Finish
This following code sample instructs the shell to open the EULA.txt file. The shell will open the program associated with the .txt file type (usually Microsoft Notepad). The program will wait indefinitely for the application to terminate. //How to wait for a shelled application to finish //Get the name of the system folder String* sysFolder= Environment::GetFolderPath(Environment::SpecialFolder::System); //Create a new ProcessInfo structure ProcessStartInfo * pInfo = new ProcessStartInfo; //Set the file name member of the process info structure. pInfo->FileName = String::Concat(sysFolder, S&quot;\\eula.txt&quot;); //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; //Console::WriteLine(S&quot;Code continuing...&quot;); MessageBox::Show(S&quot;Code continuing...&quot;); back to the top

Option 2: 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 = String::Concat(sysFolder, S&quot;\\eula.txt&quot;); //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(&quot;Code continuing...&quot;); back to the top

Choosing Between the Two Options
It may be difficult to choose between these two options. The primary purpose of a time-out is to avoid having your application stop responding (&quot;hang&quot;) because the other application has stopped responding (&quot;hung&quot;). 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.

back to the top

