Reputation: 21684
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.
Upvotes: 5
Views: 3397
Reputation: 1148
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.
Upvotes: 5
Reputation: 59
The answer of this question shows how to kill all subprocesses of a process recursively. Instead of killing it, you can wait for each of the subprocesses until it finishes. You could use the Process.Exited event as described in this question, which looks like a duplicate to this one.
Upvotes: 1
Reputation: 83
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
Upvotes: 2