使用C#StandardOutput.ReadLine()应用程序挂起 [英] StandardOutput.ReadLine() hang the application using c#

查看:1163
本文介绍了使用C#StandardOutput.ReadLine()应用程序挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是应用code。一些时间p.StandardOutput.ReadLine();工作正常,但过一段时间就挂断我尝试了所有的东西,但仍然收到此错误

Following is the application code. some time p.StandardOutput.ReadLine(); works fine but some time it hang up i tried all things but still getting this error

 ProcessStartInfo startInfo = new ProcessStartInfo("c:\\windows\\system32\\test.exe");
 String s = " ";

 startInfo.WindowStyle = ProcessWindowStyle.Hidden;
 startInfo.CreateNoWindow = true;
 startInfo.RedirectStandardInput = true;
 startInfo.RedirectStandardOutput = true;
 startInfo.UseShellExecute = false;
 Process p = Process.Start(startInfo);
 p.StandardInput.WriteLine("list volume\n");
 String f = "";
 bool ignoredHeader = false;

 s = p.StandardOutput.ReadLine();
 p.WaitForExit();

请帮我

推荐答案

您计划是正确的,如果被调用的程序总是输出只有一个行,该行是不是由系统所采用的缓冲短。

Your program is correct if the called program always outputs exactly one line, and that line is shorter than the buffer employed by the system.

如果它不输出线,的ReadLine 将不会返回。所以,你的程序在这种情况下,打破了。

If it doesn't output a line, ReadLine won't return. So your program is broken in this case.

如果它输出太多,输出缓冲区运行满,被调用的程序将阻止其电话,直到有人读取输出不够。因为你从来没有从超越第一行输出缓冲区读取,此块将永远持续下去,因此,所谓的程序将永远不会终止。这反过来会导致你的程序僵局 p.WaitForExit()

If it outputs too much, the output buffer runs full, and the called program will block on its Write call, until somebody reads enough from the output. Since you never read from the output buffer beyond the first line, this block will last forever, and thus the called program will never terminate. This in turn causes your program to deadlock at p.WaitForExit().

借助文档明确规定:

阅读其重定向流的末尾之前,不要等到子进程退出。

Do not wait for the child process to exit before reading to the end of its redirected stream.

在code例如通过调用避免死锁条件 p.StandardOutput.ReadToEnd p.WaitForExit 。可能会导致死锁条件如果父进程调用 p.WaitForExit p.StandardOutput.ReadToEnd 和子进程写入前足够多的文本来填充重定向流。父进程将无限期地等待子进程退出。子进程将无限期地等待父母从完整的 StandardOutput 流中读取。

The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.

这篇关于使用C#StandardOutput.ReadLine()应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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