C#获取过程输出运行时 [英] C# get process output while running
本文介绍了C#获取过程输出运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
反正是有重定向衍生进程的标准输出和捕获它作为其发生。我所看到的一切都只是做的过程完成后ReadToEnd的。我希望能够得到尽可能正在打印它的输出
编辑:
私人无效ConvertToMPEG()
{
//启动子进程。
进程p =新工艺();
//重定向子进程的输出流。
p.StartInfo.UseShellExecute = FALSE;
p.StartInfo.RedirectStandardOutput = TRUE;
//设置文件名和参数
p.StartInfo.Arguments =的String.Format( - Ÿ-i \{0} \-target NTSC-DVD -sameq -s 720×480 \ {1} \,的tempDir +out.avi的tempDir +out.mpg);
p.StartInfo.FileName =ffmpeg.exe;
//处理接收的数据
p.OutputDataReceived + =新DataReceivedEventHandler(p_OutputDataReceived);
p.Start();
}
无效p_OutputDataReceived(对象发件人,DataReceivedEventArgs E)
{
的Debug.WriteLine(e.Data);
}
解决方案
使用的从过程Process.OutputDataReceived 事件,要收到你所需要的数据
例如:
VAR MYPROC =新工艺();
...
myProc.StartInfo.RedirectStandardOutput = TRUE;
myProc.OutputDataReceived + =新DataReceivedEventHandler(MyProcOutputHandler);
:
私人静态无效MyProcOutputHandler(对象sendingProcess,
DataReceivedEventArgs轮廓)
{
//收集sort命令的输出。
如果(!String.IsNullOrEmpty(outLine.Data))
{
....
}
}
Is there anyway to redirect standard output of a spawned process and capture it as its happening. Everything I have seen just does a ReadToEnd after the process has finished. I would like to be able to get the output as it is being printed.
Edit:
private void ConvertToMPEG()
{
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
//Setup filename and arguments
p.StartInfo.Arguments = String.Format("-y -i \"{0}\" -target ntsc-dvd -sameq -s 720x480 \"{1}\"", tempDir + "out.avi", tempDir + "out.mpg");
p.StartInfo.FileName = "ffmpeg.exe";
//Handle data received
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.Start();
}
void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Debug.WriteLine(e.Data);
}
解决方案
Use Process.OutputDataReceived event from the process, to recieve the data you need.
Example:
var myProc= new Process();
...
myProc.StartInfo.RedirectStandardOutput = true;
myProc.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
...
private static void MyProcOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
....
}
}
这篇关于C#获取过程输出运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文