在一个单一的控制台线工艺输出重定向 [英] Process output redirection on a single console line

查看:100
本文介绍了在一个单一的控制台线工艺输出重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写可以启动第三方命令行可执行文件和标准输出重定向到一个RichTextBox C#应用程序。

I am writing a C# application that can launch third party command line executables and redirect their standard output to a RichTextBox.

我能够重定向输出使用process.OutputDataReceived事件好吗。我有我行推到一个队列,然后我用一个定时器定期转储所有排队的行到RichTextBox。

I am able to redirect the output alright by using the process.OutputDataReceived event. I have a queue that I push lines to, and then I use a timer to periodically dump all the queued lines into the RichTextBox.

这完全适用于某些第三方程序。

This works perfectly for some third party programs.

问题在于与写入控制台多次的单个行程序。例如:5% - > 10% - > 25%等......(不断改写的相同点)
这样做,我想,通过不把换行符,而是简单地返回到了回车行的开头,写在previous字符。

The problem lies with programs that write to a single line of the console multiple times. eg: 5% -> 10% -> 25% etc... (constantly rewriting the same spot) This is done, I suppose, by not putting a line feed character, but rather simply returning to the beginning of the line with a carriage return and writing over the previous characters.

会发生什么事是,OutputDataReceived似乎并不具有指示新行是否应适用或不那么我RichTextBox的输出只需对新生产线的多个更新的任何方式。
例如:
5%
10%
25%
等等...

What happens is that the OutputDataReceived does not seem to have any way of indicating if a new line should be applied or not so my RichTextBox output simply has multiple updates on new lines. eg: 5% 10% 25% etc...

有没有办法判断标准输出上一个新行或同一行了?

Is there any way to tell if the standard output is going on a new line or the same line?

推荐答案

如果我理解正确,你正在看报的的RichTextBox 控制,各行最新进展,当你想效仿控制台窗口的行为。即覆盖用新的previously输出线,而不是保持在previous线和添加一个新的。考虑到这一点…

If I understand correctly, you are currently seeing progress updates reported to your RichTextBox control as individual lines, while you would like to emulate the behavior of the console window. I.e. to overwrite the previously output line with the new one, rather than keeping the previous line and adding a new one. With that in mind…

在.NET类型,包括像的TextReader 过程类的处理输出,考虑一个换行符是任何 \\ r 的\\ n \\ r \\ n 。因此,即使是简单的使用过程中 \\ r 来覆盖当前线仍然是由 OutputDataReceived 作为开启新行。鉴于这种行为怎么是一致的跨.NET,你的唯一真正的选择是避免使用任何内置的基于行的I / O机制。

The .NET types, including things like TextReader and the Process class's handling of output, consider a newline to be any of \r, \n, or \r\n. So even a process that is simply using a \r to overwrite the current line would still be interpreted by OutputDataReceived as starting a new line. Given how consistent this behavior is across .NET, your only real option is to avoid using any built-in line-based I/O mechanisms.

幸运的是,你可以得到异步从过程中直接​​通过 StandardOutput 读取,而不必处理基于行的 OutputDataReceived 事件。例如:

Fortunately, you can get asynchronous reads from the process directly via StandardOutput, instead of having to handle the line-based OutputDataReceived event. For example:

async Task ConsumeOutput(TextReader reader, Action<string> callback)
{
    char[] buffer = new char[256];
    int cch;

    while ((cch = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)
    {
        callback(new string(buffer, 0, cch));
    }
}

您可以称之为是这样的:

You might call it like this:

// don't "await", but do keep "task" so that at some later time, you can
// check the result, "await" it, whatever, to determine the outcome.
Task task = ConsumeOutput(process.StandardOutput, s =>
    {
        richTextBox1.AppendText(s);
    });

当然,没有任何修饰上面只会做你已经获得。但读取输出这种方式不会做换行字符的任意跨pretation。相反,他们将字符串present 取值传递到匿名方法处理每读回调。所以,你可以自己扫描字符串,寻找独行回车即表明您正在开始一个新行,但添加新的文本之前应该删除previous线。一定要添加(或至少跳过)所有文本到回车的第一和然后的删除的最后一行,添加新的案文。

Of course, without any modification the above will just do what you are already getting. But reading the output this way will not do any interpretation of the line-break characters. Instead, they will be present in the string s that is passed to the anonymous method handling each read callback. So you can scan that string yourself, looking for lone carriage returns that indicate that you are starting a new line, but should delete the previous line before adding the new text. Be sure to add (or at least skip) all of the text up to the carriage return first, and then delete the last line, before adding the new text.

这篇关于在一个单一的控制台线工艺输出重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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