加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#实时显示Process的输出

发布时间:2020-12-15 04:13:06 所属栏目:百科 来源:网络整理
导读:参见英文答案 Redirect console output to textbox in separate program5个 C# – Capturing Windows Application Output3个 在C#中,我正在启动第三方应用程序,需要2-3个小时才能完成.我需要Process的输出实时写入控制台.我已经从微软的网站上研究了BeginOut
参见英文答案 > Redirect console output to textbox in separate program5个
> C# – Capturing Windows Application Output3个
在C#中,我正在启动第三方应用程序,需要2-3个小时才能完成.我需要Process的输出实时写入控制台.我已经从微软的网站上研究了BeginOutputReadLine()和RedirectStandardOutput,但我的代码仍然没有用.

目前我的代码只显示进程完成时的输出.我不知道它出了什么问题.

static void Main(string[] args)
{
  Process process;
  process = new Process();
  process.StartInfo.FileName = "C:ffmbcffmbc.exe";
  process.StartInfo.Arguments = "-i \dssp-isi-tTMDB002C010_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-tMTPO_TransferdbayB002C010_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);
}

解决方法

这条线
process.WaitForExit();

将导致当前程序等待,直到给定的进程完成.这肯定不是你想要的;你可能想要启动它,让它以异步方式运行,然后让它告诉你它什么时候结束.为此,您将需要使用process.Exited事件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读