我有一个经常使用外部程序并读取其输出的程序。 通常使用进程重定向输出它可以很好地工作,但是当我尝试读取一个特定的参数时却出现了挂起的情况,没有错误消息-没有异常,当它到达那一行时就会“停止”。 当然,我使用了一个集中式函数来调用和读取程序的输出,如下:
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")时才会出现。所有其他参数正常工作-它读取值并将其返回。
它挂起的方式也很奇怪。它不会冻结或报错,它只是停止处理。就好像是一个“返回”命令一样,但它甚至不会返回到调用函数,它只是停止了一切,接口仍然保持运行状态。
有人以前遇到过这种情况吗?有人知道我应该尝试什么吗?我认为这是流本身中的某些意外情况,但是否有办法处理/忽略它,使它仍然可以读取?