Process.Exited is never called, even though EnableRaisingEvents is set to true

前端 未结 2 1556
悲哀的现实
悲哀的现实 2020-12-11 16:58

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.Exit

相关标签:
2条回答
  • 2020-12-11 17:24

    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 call WaitForExit(). If you want to read without blocking the current thread, you can use BeginOutputReadLine() and the OutputDataReceived events.

    0 讨论(0)
  • 2020-12-11 17:25

    Apparently you have to listen to the StandardOutput stream if you redirected it, otherwise the Process will not exit. It waits for someone to read the output first.

    Process.Exited event is not be called

    0 讨论(0)
提交回复
热议问题