Process sometimes hangs while waiting for Exit

前端 未结 4 1244
天命终不由人
天命终不由人 2021-01-11 10:23

What may be the reason of my process hanging while waiting for exit?

This code has to start powershell script which inside performs many action e.g start recompiling

4条回答
  •  既然无缘
    2021-01-11 10:28

    Not sure if this is your issue, but looking at MSDN there seems to be some weirdness with the overloaded WaitForExit when you are redirecting output asynchronously. MSDN article recommends calling the WaitForExit that takes no arguments after calling the overloaded method.

    Docs page located here. Relevant text:

    When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this method returns. To ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter after receiving a true from this overload. To help ensure that the Exited event is handled correctly in Windows Forms applications, set the SynchronizingObject property.

    Code modification might look something like this:

    if (process.WaitForExit(ProcessTimeOutMiliseconds))
    {
      process.WaitForExit();
    }
    

提交回复
热议问题