I\'ve a problem in my project. I would like to launch a process, 7z.exe (console version). I\'ve tried three different things:
The problem is caused by calling the Process.WaitForExit method. What this does, according to the documentation, is:
Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited. To avoid blocking the current thread, use the Exited event.
So, to prevent the thread blocking until the process has exited, wire up the Process.Exited event handler of the Process object, as below. The Exited event can occur only if the value of the EnableRaisingEvents property is true.
process.EnableRaisingEvents = true;
process.Exited += Proc_Exited;
private void Proc_Exited(object sender, EventArgs e)
{
// Code to handle process exit
}
Done this way, you will be able to get the output of your process, while it is still running, via the Process.OutputDataReceived event as you currently do. (PS - the code example on that event page also makes the mistake of using Process.WaitForExit.)
Another note is that you need to ensure your Process object is not cleaned up before your Exited method can fire. If your Process is initialized in a using statement, this might be a problem.