【发布时间】:2013-07-10 20:04:42
【问题描述】:
在 C# 中,我正在启动一个需要 2 到 3 小时才能完成的第 3 方应用程序。我需要 Process 的输出实时写入控制台。我已经对微软网站上的BeginOutputReadLine() 和RedirectStandardOutput 进行了研究,但我的代码仍然无法正常工作。
目前我的代码仅在进程完成时显示输出。不知道哪里出了问题。
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);
}
【问题讨论】:
-
@Xeano 不完全相同的问题,但是是的,非常相似。
-
这很正常,当你重定向它的输出时,进程会切换到缓冲输出。如果它没有吐出很多文本,那么该缓冲区就不会填满足以导致它被刷新。如果您无法修复程序的代码,您将无能为力。
标签: c#