【发布时间】:2011-11-01 21:02:42
【问题描述】:
我有一个程序经常使用外部程序并读取其输出。 使用您通常的流程重定向输出它工作得很好,但是当我尝试阅读它时,一个特定的参数由于某种原因挂起,没有错误消息 - 没有例外,它只是在到达该行时“停止”。 我当然使用一个集中的函数来调用和读取程序的输出,就是这样:
public string ADBShell(string adbInput)
{
try
{
//Create Empty values
string result = string.Empty;
string error = string.Empty;
string output = string.Empty;
System.Diagnostics.ProcessStartInfo procStartInfo
= new System.Diagnostics.ProcessStartInfo(toolPath + "adb.exe");
procStartInfo.Arguments = adbInput;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
procStartInfo.WorkingDirectory = toolPath;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
proc.WaitForExit();
result = proc.StandardOutput.ReadToEnd();
error = proc.StandardError.ReadToEnd(); //Some ADB outputs use this
if (result.Length > 1)
{
output += result;
}
if (error.Length > 1)
{
output += error;
}
Return output;
}
catch (Exception objException)
{
throw objException;
}
}
挂起的行是result = proc.StandardOutput.ReadToEnd();,但同样,不是每次,只有在发送特定参数(“start-server”)时。所有其他参数都可以正常工作 - 它读取值并返回它。
它的悬挂方式也很奇怪。它不会冻结或给出错误或任何东西,它只是停止处理。就好像它是一个“返回”命令,除了它甚至不返回调用函数,它只是停止一切,接口仍然启动并运行。
以前有人经历过吗?有人知道我应该尝试什么吗?我假设它在流本身中是出乎意料的,但是有没有办法我可以处理/忽略它以便它仍然读取它?
【问题讨论】:
-
这是我的[相同]问题得到解答的地方:stackoverflow.com/questions/139593/…
-
您可能对this post 感兴趣,它解释了如何使用 .NET 进程流处理死锁。 MedallionShell 库,简化了进程 io 流的处理
-
在控制台中以相同的参数运行相同的程序。它在警告后提示用户交互,例如输入密码或确认,这可能是原因。例如,pgAdmin(postgress 数据库管理)挂起,询问不在其配置文件中的数据库的密码。但这只能从控制台运行看到
-
如果你用过输入流,这个答案适合你:stackoverflow.com/a/29118547/6859121
标签: c# stream freeze redirectstandardoutput