StandardOutput.EndOfStream挂起 [英] StandardOutput.EndOfStream Hangs

查看:894
本文介绍了StandardOutput.EndOfStream挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始运行在控制台应用程序我的C#应用​​程序中的过程。我重定向标准输入和输出,以及我能够通过StandardOutput.ReadLine读了几行()。我相信我有正确的ProcessStartInfo配置。

I'm starting a process within my C# application which runs a console application. I've redirected standard input and output, and am able to read a few lines via StandardOutput.ReadLine(). I'm convinced I have the ProcessStartInfo configured correctly.

控制台应用程序,启动时,输出几行(用标记行结束),然后等待输入。接收输入后,将其再次输出几行(具有标记线再次结束),等等。我的本意是,直到我收到标记路线,在这一点上,我知道要发送相应的输入字符串读取它的行。

The console application, when started, outputs a few lines (ending with a "marker" line) and then waits for input. After receiving the input, it again outputs a few lines (ending again with a "marker" line), and so on. My intention is to read lines from it until I receive the "marker" line, at which point I know to send the appropriate input string.

我的问题是,经过反复几次,程序挂起。暂停调试器往往放置杭StandardOutput.EndOfStream通话中。这是在下面的测试代码的情况:

My problem is that, after several iterations, the program hangs. Pausing the debugger tends to place the hang within a call to StandardOutput.EndOfStream. This is the case in the following test code:

while (!mProcess.StandardOutput.EndOfStream) // Program hangs here.
{
    Console.WriteLine(mProcess.StandardOutput.ReadLine());
}

在我测试的标记行了,我得到相同的那种挂,如果我试图读线路后访问StandardOutput.EndOfStream:

When I'm testing for the "marker" line, I get the same kind of hang if I attempt to access StandardOutput.EndOfStream after reading the line:

string line = "";
while (!isMarker(line))
{
    line = mProcess.StandardOutput.ReadLine();
}
bool eos = mProcess.StandardOutput.EndOfStream; // Program hangs here.



什么可能我会做,导致该属性如此可怕的表演?

What might I be doing that causes this property to perform so horribly?

推荐答案

您不能可靠地在这里使用EndOfStream。该StreamReader.EndOfStream属性将调用StandardOutput.Read(),如果它不具有缓冲的任何字符。如果进程不发送任何东西到它的输出管道,并没有关闭它那read()调用将阻塞。这是相当多的一定会发生的,因为它会等待输入。直到该进程已关闭其输出管道的结束和StreamReader的已经消耗了所有缓冲字符EndOfStream不会返回true。在程序终止。

You cannot use EndOfStream reliably here. The StreamReader.EndOfStream property will call StandardOutput.Read() if it doesn't have any characters buffered. That Read() call will block if the process isn't sending anything to its output pipe and doesn't close it. Which is pretty much guaranteed to happen since it will be waiting for input. EndOfStream won't return true until the process has closed its end of the output pipe and the StreamReader has consumed all its buffered characters. At program termination.

使用BeginOutputReadLine()可能是一个更好的办法来检测标记行。谨防回调发生在另一个线程。还要注意的是,直到进程准备读它应该没有必要等待进程发送标记,任何你写将被缓冲。要注意的是缓冲区是个头不大,僵局是可能的。

Using BeginOutputReadLine() might be a better way to detect the "marker" line. Beware that the callback happens on another thread. Also note that it shouldn't be necessary to wait for the process to send the marker, anything you write will be buffered until the process is ready to read it. Beware that the buffers are smallish, deadlock is possible.

这篇关于StandardOutput.EndOfStream挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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