Question

I have an application that launches another program and monitors it. When the program closes, my application also closes.

However, if I close my application first, the other program is still running.

ProcessStartInfo procInfo = new ProcessStartInfo("myProg.exe");
Process proc = new Process();
proc.StartInfo = procInfo;
proc.Start();

while (!proc.HasExited)
{
    // do stuff
}

// On proc exit, my application is also done

How do I make sure that if I close my monitoring app, any processes that are being monitored are also killed?

So for example suppose MyApp is monitoring Notepad. If I close MyApp, Notepad should also be closed.

Was it helpful?

Solution

Since this is a console application, capturing the "Exit" event is a bit trickier than forms. Take a look at this thread, I have it bookmarked because I was wondering the same thing as you a long time ago and it's handy to keep this around.

OTHER TIPS

At that end of your program call:

if (!proc.HasExited)
    proc.CloseMainWindow();

... to close the other process's window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top