有没有办法重定向一个衍生进程的标准输出并在它发生时捕获它。我看到的所有内容都是在进程完成后使用ReadToEnd。我想要能够在输出正在打印时获取它。
编辑:
编辑:
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);
}
RedirectStandardOutput
设置为 true 才能使其正常工作。 - vcsjonesmyProc.BeginOutputReadLine();
。 - MarkusdataReceivedEventHandler
的文档中提到:“当重定向的流被关闭时,会向事件处理程序发送一个空行。在访问 Data 属性之前,请确保您的事件处理程序检查此条件。例如,您可以使用静态方法 String.IsNullOrEmpty 来验证事件处理程序中的 Data 属性。” - RToyo