Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Show output of Process in real time [duplicate]

Tags:

c#

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);
}
like image 789
user2475310 Avatar asked Jul 10 '13 20:07

user2475310


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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.

like image 180
Gray Avatar answered Sep 19 '22 16:09

Gray