I have an executable, which runs fine when i run it manually, and it exists as it should with the expected output. But when i run it with the method bellow, the Process.Exited event is never fired. Notice that i have remembered the Process.EnableRaisingEvents
protected override Result Execute(RunExecutable task)
{
var process = new Process();
process.StartInfo.Arguments = task.Arguments;
process.StartInfo.FileName = task.ExecutablePath;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.Exited += (sender, args) =>
{
processSync.OnNext(new Result
{
Success = process.ExitCode == 0,
Message = process.StandardOutput.ReadToEnd()
});
processSync.OnCompleted();
};
process.Start();
return processSync.First();;
}
The problem is the same, if i use Process.WaitForExit() instead of reactive extensions, to wait for the exit event.
Also, if i run the process with another argument, which produces another output, it exists fine.
It seems to have something to do with the process.StartInfo.RedirectStandardOutput = true; since when i disable this, it works. But that could just be a symptom of another problem.
Any help is appreciated 🙂
There’s a deadlock in your code. ‘Standard output’ is just a kind of named pipe, which has a small buffer to transfer data from one process to the other. If the buffer is full, the writing process has to wait for the reading process to retrieve some data from the buffer.
So the process you have started might wait for you to read from the standard output, but you are waiting for the process to finish before you start reading -> deadlock.
The solution is to read continuously while the process is running – just call
StandardOutput.ReadToEnd()before you callWaitForExit().If you want to read without blocking the current thread, you can use
BeginOutputReadLine()and theOutputDataReceivedevents.