问题 C#运行时获取进程输出


无论如何,重定向生成的进程的标准输出并将其捕获为它的发生。我看到的所有内容在完成后都会执行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);
    }

7608
2017-08-16 20:00


起源



答案:


使用 Process.OutputDataReceived 来自流程的事件,以接收您需要的数据。

例:

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))
    {
      ....    
    }
}

13
2017-08-16 20:04



是的,此外你需要设置 RedirectStandardOutput 真的让这个工作。 - vcsjones
@vcsjones:只是在写另外的帖子。 - Tigran
我试过了,但我没有运气。它运行正常但回调从未被击中。也许这是输出打印方式的问题。我和ffmpeg一起使用它。我将我的代码添加到原始帖子中。 - thecaptain0220
我也不得不补充一下 myProc.BeginOutputReadLine(); 在我开始这个过程之后。 - Markus
@lii dataReceivedEventHandler 文档说:“当重定向流关闭时,会向事件处理程序发送一个空行。确保您的事件处理程序在访问Data属性之前检查此条件。例如,您可以使用静态方法String.IsNullOrEmpty来验证事件处理程序中的Data属性。“ - RToyo


答案:


使用 Process.OutputDataReceived 来自流程的事件,以接收您需要的数据。

例:

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))
    {
      ....    
    }
}

13
2017-08-16 20:04



是的,此外你需要设置 RedirectStandardOutput 真的让这个工作。 - vcsjones
@vcsjones:只是在写另外的帖子。 - Tigran
我试过了,但我没有运气。它运行正常但回调从未被击中。也许这是输出打印方式的问题。我和ffmpeg一起使用它。我将我的代码添加到原始帖子中。 - thecaptain0220
我也不得不补充一下 myProc.BeginOutputReadLine(); 在我开始这个过程之后。 - Markus
@lii dataReceivedEventHandler 文档说:“当重定向流关闭时,会向事件处理程序发送一个空行。确保您的事件处理程序在访问Data属性之前检查此条件。例如,您可以使用静态方法String.IsNullOrEmpty来验证事件处理程序中的Data属性。“ - RToyo


因此,经过一点挖掘,我发现ffmpeg使用stderr进行输出。这是我修改后的代码来获取输出。

        Process p = new Process();

        p.StartInfo.UseShellExecute = false;

        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;

        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";

        p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
        p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

        p.Start();

        p.BeginErrorReadLine();
        p.WaitForExit();

3
2017-08-16 21:00