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

Article translations Article translations
Article ID: 307011 - View products that this article applies to.
This article was previously published under Q307011
Expand all | Collapse all

On This Page

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.

Requirements

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

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)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx
  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.

REFERENCES

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

Visual C++ .NET (2002) Support Center

Properties

Article ID: 307011 - Last Review: January 12, 2006 - Revision: 2.2
APPLIES TO
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbnewsgrouplink KB307011

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