Is there a wait method which would return when the target process and all its sub-processes exited? It seems Process.WaitForExit() would only wait on on target process.
3 Answers
There is a bug in .net that will give the behaviour you are after.
WaitForExit()
will wait for all child processes if you are reading the output asynchronously.
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived = new DataReceivedEventHandler(OutputHandler);
p.BeginOutputReadLine();
p.BeginErrorReadLine();
WaitForExit(Int32.MaxValue-1)
allows us to get normal behaviour when using async mode.

- 1,089
- 14
- 17
This might help a bit - Windows 2000 introduced the concept of a "job" which is a collection of processes controlled as ONE unit quite like a process group in UNIX/LINUX. So you can kill a process and all its children by launching the process in a job, then telling Windows to kill all processes in the job.
Basically create a new job, then use the job to spawn the child process. All its children will also be created in the new Job. When you time out, just call the job's kill() method and the entire process tree will be terminated. You might way to check the .NET documentation for more information on this. I used the Perl's Win32::Job API to achieve what you are trying to do. Check out the following link on MSDN for more info: Job API Description

- 83
- 5
-
This is what WaitForExit() does. Clearly this does not solve Thomson's (or my )issue. My problem, specifically is to initiate a windows form from another one (the spawned application is a click-once -- so it is invoked using the .application file) The Process.Exited flag is set immediately. Probably as soon as the actual forms application is launched. – Mark Ainsworth Sep 13 '15 at 16:16