0

In .NET, there is the class System.Diagnostics.Process. It has the property HasExited, which should tell me whether the process has terminated. However, the documentation says "When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this property returns true. To ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter before checking HasExited." (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.hasexited?view=netframework-4.5#system-diagnostics-process-hasexited).

What does "standard output has been redirected to asynchronous event handlers" mean? I don't know this. And, because of this, it seems that this property is not a good way to determine whether a process has terminated. The documentation recommends "call the WaitForExit() overload that takes no parameter before checking HasExited". But that method will wait until the process and event handlers to exit, which is not to simply determine whether the process (and the strange "event handlers") has exited.

So, in .NET, is there a good way to determine whether a process (including those strange "event handlers") has terminated?

zhoudu
  • 623
  • 8
  • 19
  • Why do you think this property is not good? Do you really want to determine whether a process has terminated or for other purposes? There is also a WaitForExitAsync method if you prefer asynchronization. – shingo Sep 02 '23 at 11:28
  • If you use the *asynchronous* version (event driven), you set `EnableRaisingEvents = true`, which then raises the `Exited` event when the Process exits (in this case, you don't call `WaitForExit()`, of course). Or, if you need to wait asynchronously, do as mentioned in the previous comment, if `WaitForExitAsync()` is available (.NET 5+) – Jimi Sep 02 '23 at 12:33
  • The following may be of interest: https://stackoverflow.com/a/71846819/10024425 – Tu deschizi eu inchid Sep 02 '23 at 13:35
  • https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardoutput – Charlieface Sep 03 '23 at 01:36

0 Answers0