如何在 Process.StandardOutput 中禁用输出缓冲

发布于 2024-09-25 11:14:11 字数 696 浏览 3 评论 0原文

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

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

调用 Process.StandardOutput.BaseStream.Flush() 不起作用。

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

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

无论如何,这必须完成,即使我最终编写非托管 C++ 来启动任务并使用输出,并将其链接进来。非常感谢任何帮助;我束手无策...

编辑:看来我需要的是“期望”库的 .Net 实现,这些库可用于 C/C++、Perl、Python 和 Java(这些是我唯一使用的库)到目前为止已经找到)。有谁知道这样的野兽是否存在?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2024-10-02 11:14:11

“[我]有没有办法启动它,这样它就不会意识到自己正在被重定向?”是的:这正是 Expect 的领域。据我所知,没有 .Net 实现;这当然是可行的,不过...

"[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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文