Microsoft KB Archive/307011

= How to determine when a shelled process ends in Visual C++ =

Article ID: 307011

Article Last Modified on 1/12/2006

-

APPLIES TO


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

-



This article was previously published under Q307011



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

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

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

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Sample



SUMMARY
This article demonstrates how to use the Process object to start another process and receive a notification after that process has exited. The Process object, which is located in the System::Diagnostics namespace, exposes the Exited event. By handling this event, you are notified when the process that you started has exited. Because you are using an event to accomplish this task, this notification process is asynchronous.

back to the top

Requirements

 * Visual Studio .NET or Visual Studio 2005
 * Visual C++ Language Compiler

back to the top

Create the Sample
 Create a new .NET Managed C++ application project.

Note In the New Project dialog box, click Visual C++ Projects under Project Types, and then click Managed C++ Application for Visual Studio .NET 2002, click Console Application (.NET) for Visual Studio .NET 2003, or click CLR Console Application for Visual Studio 2005 under Templates.  Double-click .cpp in the Solution Explorer. Remove all of the existing code and paste in the following code:
 * 1) include &quot;stdafx.h&quot;
 * 2) using 


 * 1) using 

using namespace System; using namespace System::Diagnostics;

int event_fired;

public __gc class X { public: static void p_Exited(Object *sender, System::EventArgs *e) {       Console::WriteLine(&quot;Notepad was closed.&quot;); event_fired = true; } };

int main { System::Diagnostics::Process *p; p = new System::Diagnostics::Process; p->Exited += new System::EventHandler(0,&X::p_Exited); p->EnableRaisingEvents = true;

ProcessStartInfo *pSi= new ProcessStartInfo; pSi->FileName = &quot;notepad.exe&quot;; p->set_StartInfo(pSi); p->Start; event_fired = false; while(event_fired== false){}; // wait for the event return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:  Click Project, and then click  Properties.

Note  is a placeholder for the name of the project. Expand Configuration Properties, and then click General. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.</ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Press F5 to run the application. The application should start, along with an instance of Notepad.</li> Close Notepad. Note that the Exited event fires, which causes a message box to appear in the console window.</li></ol>

back to the top