Reputation: 137492
I found some code like this. It runs a process then prints what it wrote to standard output. The problem is, that it doesn't print anything until the process has exited which might take a very long time (or not at all). Thus, I'd rather print the output AS IT GOES. How can I do that?
var startInfo = new ProcessStartInfo("cmd", "/c sleepy.bat")
{RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true };
var p = new Process(){StartInfo = startInfo};
p.Start();
string output = p.StandardOutput.ReadToEnd();
Console.WriteLine(output);
Where sleepy.bat
could be
echo "About to execute really slow query.."
sleep 20
echo "Finished!"
Upvotes: 2
Views: 103
Reputation: 12654
Try using p.StandardOutput.BeginOutputReadLine()
. You will receive output as it goes from the process in OutputDataReceived
event.
For extended example, take a look at MSDN article.
Upvotes: 2