I am starting a process (actually phantomjs) using c#, and am trying to pass information (a base64image string) via the standard output. Should the process succeed, everything goes well. Should the process fail (in this case because there is a javascript error in a page phantomjs is opening), it hangs indefinitely.
My code looks like this:
var path = Server.MapPath("phantomjs.exe");
var args = string.Join(" ", new[] { Server.MapPath(@"screenshot.js"), url });
var info = new ProcessStartInfo(path, args);
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
var p = Process.Start(info);
// it hangs on the following line:
var base64image = p.StandardOutput.ReadToEnd();
bytes = Convert.FromBase64CharArray(base64image.ToCharArray(), 0, base64image.Length);
I am assuming that running any external process could lead to this problem. If that process does not finish properly (for whatever reason) then there will never be an output end to read to (maybe?).
What I would like to know is, how can I introduce a maximum timeout? Should the process succeed and exit in less than the timeout, great. If not, kill the process and do something else.
I have tried the following:
if (p.WaitForExit(30000))
{
var base64image = p.StandardOutput.ReadToEnd();
bytes = Convert.FromBase64CharArray(base64image.ToCharArray(), 0, base64image.Length);
// do stuff with bytes
}
else
{
p.Kill();
// do something else
}
This worked when I was running a much simpler application (that simply wrote a number to the console every second for 60 seconds). When I tried with phantomjs, it fails (waiting 30 seconds) for cases that should work (and take much less than 30 seconds when I revert to my original code, or run it from the console).
Maybe phantomjs (or the js script I wrote) does not exit properly, but can the c# deal with all scenarios?