如何在Process.StandardOutput中禁用输出缓冲 [英] How to disable output buffering in Process.StandardOutput

查看:136
本文介绍了如何在Process.StandardOutput中禁用输出缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经被问过不止一次,但是在所有这些讨论中我都没有找到令人满意的答案.

我正在启动一个命令行过程,该过程将对STDOUT进行实时测量,大约每秒产生一次新结果.当STDOUT数据通过Process.StandardOutput StreamReader中的4k缓冲区工作时,使用System.Diagnostics.Process.StandardOutput会导致完全不可接受的延迟(超过20秒),并且似乎没有任何方法可以解决此问题. /p>

调用Process.StandardOutput.BaseStream.Flush()无效.

我尝试对Process.StandardOutput进行逐字节同步读取,但是实际输出仍然落后4k.

至少有人可以为我验证是否可以以某种方式克服重定向STDOUT时遇到的所有缓冲问题,并在应用程序中的数据一出现在shell窗口中就立即接收?我可以继承Process类并更改StandardOutput流读取器的行为吗?我需要查看原始的WINAPI调用吗?

无论如何,即使我最终编写了非托管的C ++以启动任务并使用输出并将其链接在一起,也必须完成此操作.非常感谢您的帮助.我在机智的尽头...

看来,我需要的是.net实现的期望"库,这些库可用于C/C ++,Perl,Python和Java(到目前为止,我才发现这些库).有人知道这种野兽是否存在吗?

解决方案

"[[]我是否有一种方法可以启动它,使得它[不]意识到它正在被重定向?"是的:这正是Expect的领域.我知道没有.Net实现;当然,这是可行的...

This question has been asked more than once before, but I have not found a satisfactory answer in any of those discussions.

I am launching a command-line process that produces a real-time measurement to STDOUT, producing a new result approximately every second. Using System.Diagnostics.Process.StandardOutput results in completely unacceptable lag (over 20 seconds) as the STDOUT data works through the 4k buffer in the Process.StandardOutput StreamReader, and there doesn't seem to be any way to get around this.

Calling Process.StandardOutput.BaseStream.Flush() doesn't work.

I've tried doing a byte-by-byte synchronous read of Process.StandardOutput, but I'm still 4k behind the actual output.

Can anyone at least verify for me that it is possible to somehow overcome all of the buffering issues I am having with redirecting STDOUT, and receive the data in my application as soon as it would have appeared in the shell window? Can I inherit from the Process class and change how the StandardOutput streamreader behaves? Do I need to be looking at raw WINAPI calls?

Somehow, this has to get done, even if I end up writing unmanaged C++ to launch the task and consume the output, and linking that in. Any help is much appreciated; I'm at my wit's end...

Edit: It appears that what I need is a .Net implementation of the "expect" libraries that are available for C/C++, Perl, Python, and Java (those are the only ones I have found so far). Does anyone know if such a beast exists?

解决方案

"[I]s there a way to launch it such [that] it doesn't realize it is being redirected?" YES: that is exactly the domain of Expect. I know of no .Net implementation; it's surely feasible, though ...

这篇关于如何在Process.StandardOutput中禁用输出缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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