WaitForExit method, once it has finished waiting on the process handle itself, it checks to see if a reader for either stdout or stderr has been created; if so, and if the timeout value for the WaitForExit call is "infinite" i. The stdout and stderr streams are themselves not closed until the child processes have closed. So waiting on the end of those streams will block until that happens.
That WaitForExit should behave differently depending on whether one has called either of the methods that start the event-based reading of the streams or not, and especially given that reading those streams directly does not cause WaitForExit to behave that way, creates an inconsistency in the API that makes it much more difficult to understand and use.
While I'd personally call this a bug, I suppose it's possible that the implementor s of the Process class are aware of this inconsistency and created it on purpose. In any case, the work-around would be to read StandardOutput and StandardError directly instead of using the event-based part of the API. Though of course, if one's code were to wait on those streams, one would see the same blocking behavior until the child processes close.
For example C , because I don't know F well enough to slap a code example like this together quickly : :. Hopefully the above work-around or something similar will address the basic issue you've run into. My thanks to commenter Niels Vorgaard Christensen for directing me to the problematic lines in the WaitForExit method, so that I could improve this answer. Skip to content. But when I call Process. It creates a batch file, starts it and then prints: Process is still running Batch file is done!
Process has exited. Exit code: Calling WaitForExit It should then print: WaitForExit returned. IO open System. Diagnostics open System. WriteAllText "foo. Data p. AddHandler onOutput p. BeginErrorReadLine p. BeginOutputReadLine while not p. HasExited do printfn "Process is still running Sleep printfn "Process has exited. If I close stdout before waiting for exit, I won't get the output.
If I wait for exit before closing, then the program hangs. Then you need to redirect the output of process 3 , otherwise you can't know when process2 output ends. Then you need to redirect the output of process 3, otherwise you can't know when process2 output ends.
Otherwise, I don't see how one would affect the other. Yes, when starting process2, process1 creates a pipe RedirectStandardOutput and both process2 and process3 stdout write to it. Am I reading this right? I must redirect the output of any process launched down the chain? I can only ensure compliance for the processes that I start directly. But if any of them launches a process of their own I'll be doomed?
You are reading it right. If you want to distinguish between the output of a direct child, and their children, those outputs need to write to different pipes. It's a bug when a program launches a child that keeps stdout open when the child isn't going to provide output especially if that child out-lives the parent process.
Not sure if this applies to what I see. No child down the line outlives its parent. All children potentially write output. Yet, the parent hangs on WaitForExit. Related API proposal: I mean you don't want grand children keeping standard output open even if they don't actively write to it. If the child and everything down the line has exited, WaitForExit should return.
Are you running in a container? Can you reproduce this? I've moved away to a different arrangement, but if I get some time I'll try to reproduce again. WaitForExit can be quirky in containers when there is no init process.
The init process reaps orphaned child processes. You can add one with docker run by adding an --init argument. I have the exact same issue on Linux that repeats when a native daemon is running my. NET Core 3. From htop I can see that the child exits, shows quickly as a Zombie and then goes away, so it looks like.
NET Core is closing the handle, but somehow ignoring that it exited. Also, the same code works fine if executed on command-line OR under Mono 6. Platform is Raspberry Pi 4 armv7. Since you are not redirecting streams, the root cause is different. Can you create a new issue? Skip to content. Star 9. New issue. Jump to bottom. Labels area-System. Milestone Future. Copy link. WriteLine e. Data ; process2.
|Forex 1 minutes incredible scalper system mechanic free||Business ipo definition|
|Forex trading money management||Please next time copy the code to the answer. If you want to distinguish between the output of a direct child, and their children, those outputs need to write to different pipes. This still hangs for me even with redirecting and reading standard output. Your solution does not need AutoResetEvent but you poll. If the process continues to output data after the timeout has been exceeded and then terminates, the outputWaitHandle and errorWaitHandle variables will be accessed after being disposed. ReadToEnd before p. Sign up using Facebook.|
|Daily forex signals on eur usd bloomberg||546|
|Redirectstandardoutput waitforexit doesnt wait||No child down the line outlives its parent. The good news in all this is that because I have been developing VB programs for a few years. StasBoyarincev Thanks, updated. There are some notes at the bottom of this MSDN doc msdn. It would however happen under Windows 8, but not under Windows 7.|
|1997 asian financial crisis||659|
WriteLine "Passed! In My testing, it worked perfectly! Text; using Nunit. ToLower ; Assert. This didn't make any sense to me, as all the documentation I had read seemed to indicate that all you had to do was redirect the standard output of the process then wait for it to be completed. After trying a few other things which didn't work, I created the StackOverflow question listed above in the notes and went home. When I got into work the next day nobody had answered it so I continued with my personal research and found that WinForms applications have the same issue!
With my new findings about WinForms I did some more googling and found a few pages with information that helped me understand what was going on:. It looks like in addition to redirecting the StandardOutput I also have to redirect the StandardInput - even if I'm not going to use it. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.
There is a similar issue when you read all text from both the standard output and standard error streams. The following C code, for example, performs a read operation on both streams. The code example avoids the deadlock condition by performing asynchronous read operations on the StandardOutput stream. A deadlock condition results if the parent process calls p.
ReadToEnd followed by p. ReadToEnd and the child process writes enough text to fill its error stream. The parent process would wait indefinitely for the child process to close its StandardOutput stream. The child process would wait indefinitely for the parent to read from the full StandardError stream.
You can use asynchronous read operations to avoid these dependencies and their deadlock potential. Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread. You are commenting using your WordPress. You are commenting using your Twitter account. You are commenting using your Facebook account.
Notify me of new comments via email. Notify me of new posts via email. Honey Mercy. Skip to content. Start Posted on April 15, by joinwind Start is called. RedirectStandardOutput to true. Otherwise, reading from the StandardOutput stream throws an exception. ReadToEnd ; p. WaitForExit ;. Share this: Twitter Facebook. Like this: Like Loading This entry was posted in IT -. Net , IT -. Net Advanced , IT -. Net Trick. Bookmark the permalink. Start Alvaro Lehoullier says:.
December 14, at pm.