How to determine when a shelled process ends in Visual C++ (307011)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
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:
IN THIS TASKSUMMARY 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 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> 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.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: - Press F5 to run the application. The application should
start, along with an instance of Notepad.
- Close Notepad. Note that the Exited event fires, which causes a message box to appear in the console
window.
back to the top
REFERENCES For more general information about Visual C++ .NET, visit the
following Microsoft newsgroup and Microsoft Web site:
Modification Type: | Major | Last Reviewed: | 1/12/2006 |
---|
Keywords: | kbHOWTOmaster kbNewsgroupLink KB307011 kbAudDeveloper |
---|
|