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

This article was previously published under Q307011
This article has been archived. It is offered "as is" and will no longer be updated.
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


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


  • Visual Studio .NET or Visual Studio 2005
  • Visual C++ Language Compiler
back to the top

Create the Sample

  1. 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.
  2. Double-click projectname.cpp in the Solution Explorer. Remove all of the existing code and paste in the following code:
    #include "stdafx.h"#using <mscorlib.dll>#using <system.dll>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("Notepad was closed.");		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 = "notepad.exe";	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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
    /clr (Common Language Runtime Compilation)
  3. Press F5 to run the application. The application should start, along with an instance of Notepad.
  4. Close Notepad. Note that the Exited event fires, which causes a message box to appear in the console window.
back to the top


For more general information about Visual C++ .NET, visit the following Microsoft newsgroup and Microsoft Web site:
vc .net vc++

Article ID: 307011 - Last Review: 01/16/2015 23:25:53 - Revision: 2.2

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • kbnosurvey kbarchive kbhowtomaster kbnewsgrouplink KB307011