如何异步读取标准输出流和标准错误流一次 [英] How to asynchronously read the standard output stream and standard error stream at once

查看:469
本文介绍了如何异步读取标准输出流和标准错误流一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲作为是在控制台读取的处理的输出中的形式(标准输出中配合标准误差在一个数据流)。有没有办法怎么办呢?

I want to read the ouput of a process in the form as is in a console (standard output is blended with standard error in one stream). Is there a way how to do it?

我想使用

ProcessStartInfo.UseShellExecute = true;  

但我不能异步读取输出。如果我设置

but then I cannot read asynchronously the output. If I set

process.ProcessStartInfo.UseShellExecute = false;  
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(partialOutputHandler);

然后我可以读取标准输出(我可以做同样的标准误差),但我不知道如何来模拟控制台的行为(输出和错误的混合)。

then I can read standard output (I can do the same for standard error) but I don't know how to simulate the behaviour of console (the blending of stdout and stderr).

注:我知道,Linux具有重定向标准错误流到标准输出流的功能,但我无法谷歌它的.NET

Note: I know that Linux has the feature of redirecting standard error stream to the standard output stream but I was unable to google it for .NET.

我要错过一些pretty的方便。

I must miss something pretty easy.

谢谢!

推荐答案

我找到了答案:

输出流缓冲。有没有办法让真正的   的插入数据流中的项目顺序。事实上,   没有什么意义,因为两个流可以写入也同   时间。它们是彼此独立的。因此,最好可以   要做的就是让连续输出的每一个,因为他们到达。

The output streams are buffered. There is no way to get the true sequential order of the items inserted into the streams. In fact it makes little sense as both streams can be written too at the same time. They are independent of each other. Therefore the best you can do is get the sequential output from each one as they arrive.

一般来说,这不是一个问题,但几乎所有的控制台应用程序使用   标准输出为输出和错误消息。错误流   所使用的一些应用程序,但这些消息一般都是重复的   在输出流中生成的错误。

Generally this is not an issue though as almost all console apps use standard output for both output and error messages. The error stream is used by some apps but the messages are generally duplicates of the errors generated in the output stream.

来源: <一个href="http://social.msdn.microsoft.com/Forums/uk/csharpgeneral/thread/192b6df7-9437-42cf-81c1-c125021735ba" rel="nofollow">http://social.msdn.microsoft.com/Forums/uk/csharpgeneral/thread/192b6df7-9437-42cf-81c1-c125021735ba

这篇关于如何异步读取标准输出流和标准错误流一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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