I use Process.Start to start a batch file. The batch file uses the "START" command to start several programs in parallel and then exits.
Once the batch file is done Process.HasExited becomes true and Process.ExitCode contains the correct exit code.
But when I call Process.WaitForExit() it hangs / never returns.
The following piece of code demonstrates the problem. It creates a batch file, starts it and then prints:
Process is still running...
Batch file is done!
Process has exited. Exit code: 123
Calling WaitForExit()...
It should then print:
WaitForExit returned.
... but it never does (even though HasExited is true and we already have an ExitCode).
open System.IO
open System.Diagnostics
open System.Threading
let foobat = """
START ping -t localhost
START ping -t google.com
ECHO Batch file is done!
EXIT /B 123
"""
File.WriteAllText("foo.bat", foobat)
use p = new Process(StartInfo = ProcessStartInfo("foo.bat",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true))
let onOutput = DataReceivedEventHandler(fun _ args -> printfn "%s" args.Data)
p.OutputDataReceived.AddHandler onOutput
p.ErrorDataReceived.AddHandler onOutput
p.Start() |> ignore
p.BeginErrorReadLine()
p.BeginOutputReadLine()
while not p.HasExited do
printfn "Process is still running..."
Thread.Sleep(1000)
printfn "Process has exited. Exit code: %d" p.ExitCode
printfn "Calling WaitForExit()..."
p.WaitForExit()|> ignore
printfn "WaitForExit returned."
I noticed that this only happens when the batch file contains "START" commands and when standard output and/or standard error are redirected.
Why does WaitForExit() never return?
What's the right way to wait for such a process to exit?
Is it safe to just poll Process.HasExited or can that result in other problems?
PS.: I just noticed that calling WaitForExit(100000) with a huge timeout (that definitely doesn't expire) returns immediately when the process exits. Wierd. Without timeout it hangs.
Process.WaitForExit Method (System.Diagnostics)Sets the period of time to wait for the associated process to exit, and blocks the current thread of execution until the time has elapsed or the process has exited.
WaitForExit(Int32) Instructs the Process component to wait the specified number of milliseconds for the associated process to exit. public: bool WaitForExit(int milliseconds); C# Copy.
This seems to be an artifact (I'd say "bug") in the specific implementation of the event-based asynchronous handling of StandardOutput and StandardError.
I noticed that while I was able to easily reproduce your problem, simply by running the code you provided (excellent code example, by the way! :) ), the process did not actually hang indefinitely. Rather, it returned from WaitForExit() once both of the child processes that had been started had themselves exited.
This seems to be an intentional part of the implementation of the Process
class. In particular, in the Process.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.e. -1
), the code actually waits for the end-of-stream on the reader(s).
Each respective reader is created only when the BeginOutputReadLine()
or BeginErrorReadLine()
method is called. 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 :) ):
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
namespace TestSO26713374WaitForExit
{
class Program
{
static void Main(string[] args)
{
string foobat =
@"START ping -t localhost
START ping -t google.com
ECHO Batch file is done!
EXIT /B 123
";
File.WriteAllText("foo.bat", foobat);
Process p = new Process { StartInfo =
new ProcessStartInfo("foo.bat")
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
} };
p.Start();
var _ = ConsumeReader(p.StandardOutput);
_ = ConsumeReader(p.StandardError);
Console.WriteLine("Calling WaitForExit()...");
p.WaitForExit();
Console.WriteLine("Process has exited. Exit code: {0}", p.ExitCode);
Console.WriteLine("WaitForExit returned.");
}
async static Task ConsumeReader(TextReader reader)
{
string text;
while ((text = await reader.ReadLineAsync()) != null)
{
Console.WriteLine(text);
}
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With