Process.Exited is never called, although EnableRaisingEvents is set to true

I have an executable that works fine when I run it manually, and it exists as expected with the expected output. But when I fire it using the method below, the Process.Exited event never fires. Note that I remember 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 started the process with a different argument that produces a different output, it exists normally.

Something seems to be related to process.StartInfo.RedirectStandardOutput = true; because when i turn it off it works. But this may just be a symptom of another problem.

Any help is appreciated :-)

+8
c # events process
source share
2 answers

There is a dead end in your code. "Standard output" is simply a named pipe that has a small buffer for transferring data from one process to another. If the buffer is full, the writer should wait for the reader to retrieve some data from the buffer.

So, the process you started may wait until you read the standard output, but you wait for the process to complete before you start reading → dead end.

The solution is to read continuously while the process is running - just call StandardOutput.ReadToEnd() before you call WaitForExit() . If you want to read without blocking the current stream, you can use the BeginOutputReadLine() and OutputDataReceived .

+6
source share

Apparently you should listen to the StandardOutput stream if you redirected it, otherwise the process will not end. He is waiting for someone to read the result first.

Process.Exited event will not be called

+1
source share

All Articles