如何通过线,因为它们发生捕获进程输出和错误路线,工艺操作过程中。 (C#) [英] How to capture a Processes STDOUT and STDERR line by line as they occur, during process operation. (C#)

查看:118
本文介绍了如何通过线,因为它们发生捕获进程输出和错误路线,工艺操作过程中。 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行一个程序(lame.exe)编码WAV文件为MP3。

I am going to execute a Process (lame.exe) to encode a WAV file to MP3.

我想这个过程的输出和错误处理对显示进度信息。

I want to process the STDOUT and STDERR of the process to display progress information.

我是否需要使用线程?我不能让我的头周围。

Do I need to use threading? I can't get my head around it.

一些简单的示例代码将不胜感激。

Some simple example code would be appreciated.

感谢

推荐答案

如果通过过程类运行,您可以重定向视频流,以便你可能会对其进行处理。您可以从stdout或stderr同步或异步读取。要启用重定向,设置相应的重定向属性真正您要重定向数据流(例如, RedirectStandardOutput )和设置 UseShellExecute 。然后,你可以启动该进程,并从流读取。您也可以养活输入重定向标准输入。

If running via the Process class, you can redirect the streams so you may process them. You can read from stdout or stderr synchronously or asynchronously. To enable redirecting, set the appropriate redirection properties to true for the streams you want to redirect (e.g., RedirectStandardOutput) and set UseShellExecute to false. Then you can just start the process and read from the streams. You can also feed input redirecting stdin.

例如,处理和打印无论进程写到标准输出同步

e.g., Process and print whatever the process writes to stdout synchronously

var proc = new Process()
{
    StartInfo = new ProcessStartInfo(@"SomeProcess.exe")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
    }
};
if (!proc.Start())
{
    // handle error
}
var stdout = proc.StandardOutput;
string line;
while ((line = stdout.ReadLine()) != null)
{
    // process and print
    Process(line);
    Console.WriteLine(line);
}

这篇关于如何通过线,因为它们发生捕获进程输出和错误路线,工艺操作过程中。 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆