16

In C# I am starting a 3rd party application that takes 2 - 3 hours to complete. I need the output of the Process to write to the console in real time. I have done research on BeginOutputReadLine() and RedirectStandardOutput from Microsoft's website but my code is still not working.

Currently my code is only showing the output when the process is finished. I don't know where its gone wrong.

static void Main(string[] args)
{
  Process process;
  process = new Process();
  process.StartInfo.FileName = "C:\\ffmbc\\ffmbc.exe";
  process.StartInfo.Arguments = "-i \\\\dssp-isi-t\\TMD\\B002C010_130520_R2R7.2398v5.mxf -an -vcodec libx264 -level 4.1 -preset veryslow -tune film -x264opts bluray-compat=1:weightp=0:bframes=3:nal-hrd=vbr:vbv-maxrate=40000:vbv-bufsize=30000:keyint=24:b-pyramid=strict:slices=4:aud=1:colorprim=bt709:transfer=bt709:colormatrix=bt709:sar=1/1:ref=4 -b 30M -bt 30M -threads 0 -pass 1 -y \\\\dss-isi-t\\MTPO_Transfer\\dbay\\B002C010_130520_R2R7.2398v5.mxf.h264";
  process.StartInfo.UseShellExecute = false;
  process.StartInfo.CreateNoWindow = true;
  process.StartInfo.RedirectStandardOutput = true;
  process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
  process.StartInfo.RedirectStandardInput = true;
  process.Start();
  process.BeginOutputReadLine();
  process.WaitForExit();
  process.Close();
}

private static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
  string line;
  line = (outLine.Data.ToString());
  Console.WriteLine(line);
}
Adrian
  • 5,603
  • 8
  • 53
  • 85
user2475310
  • 733
  • 4
  • 13
  • 19
  • @Xeano Not exactly the same question, but yes, pretty similar. – feralin Jul 10 '13 at 20:11
  • 2
    This is pretty normal, the process will switch to buffered output when you redirect its output. If it doesn't spit out a lot a text then that buffer doesn't fill up enough to cause it to be flushed. Nothing you can do about it if you can't fix the program's code. – Hans Passant Jul 10 '13 at 20:37

2 Answers2

9

Similar to a previous question I'd answered, maybe even a duplicate. See: Pipe a stream to Debug.Write()

Here's my answer (modified slightly) from that:

process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += p_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();

Then, your event handler for receiving data.

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.Write(e.Data);
}

Basically, you just need to nix the WaitForExit(), since that makes your program hang until the process completes.

Community
  • 1
  • 1
Gray
  • 7,050
  • 2
  • 29
  • 52
  • 1
    What previous question? Provide a link. And if this is just a regurgitation of that answer, your response should be a comment, not an answer. – Jim Mischel Jul 10 '13 at 20:14
  • Thanks for the advice @JimMischel, I will add a link in my answer. Sorry if my answer is not appropriate, I am still often confused about when to flag answers and when to answer them. – Gray Jul 10 '13 at 20:15
6

The line

process.WaitForExit();

will cause the current program to wait until the given process finishes. This is most certainly not what you want; you probably want to start the process, let it run asynchronously, and then let it tell you when it finishes. For that, you will want to use the process.Exited event.

feralin
  • 3,268
  • 3
  • 21
  • 37
  • 1
    When I remove process.WaitForExit(); the console runs the process in the background then closes, it still does not show me a real time output. Am I allowed a string from the 'OutputHandler' method? – user2475310 Jul 10 '13 at 21:44