StandardOutput.ReadToEnd()挂起 [英] StandardOutput.ReadToEnd() hangs

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

问题描述

我有一个经常使用的外部程序并读取其输出的程序。
它的工作原理以及使用常规工艺重定向输出pretty,但由于某种原因一个特定的参数挂起当我尝试读它,没有错误信息 - 也不例外,它只是停止,当它到达该行。
当然,我用一个集中的函数来调用,并从程序,这是本读输出:

I have a program that frequently uses an external program and reads its outputs. It works pretty well using your usual process redirect output, but one specific argument for some reason hangs when I try to read it, no error message - no exception, it just 'stops' when it reaches that line. I of course use a centralized function to call and read output from the program, which is this:

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;
    }
}

这挂线是结果= proc.StandardOutput.ReadToEnd(); ,但同样,不是每一次,发送一个特定的参数只有当(启动服务器)。所有其他的参数只是正常工作 - 它会读取值并返回。
这也是奇怪它挂的方式。它不会冻结或给出错误或任何东西,它只是停止处理。就好像它是一个回归的命令,但它甚至不返回到调用函数,它只是停止与接口一切仍然正常运行。
任何人都经历过这个?任何人有任何想法,我应该尝试一下呢?我假设它的流本身意想不到的事情,但有一种方法,我可以处理/忽略这一点,以便它读取它呢?

The line that hangs is result = proc.StandardOutput.ReadToEnd();, but again, not every time, only when sent a specific argument ("start-server"). All other arguments work just fine - it reads the value and returns it. It's also strange the way it hangs. It doesn't freeze or give an error or anything, it just stops processing. As if it was a 'return' command, except it doesn't even return to the calling function, it just stops everything with the interface still up and running. Anyone experienced this before? Anyone have any idea what I should try? I'm assuming it's something unexpected within the stream itself, but is there a way I can handle/ignore this so that it reads it anyway?

推荐答案

埃拉德!
两年过去了,我面临同样的问题。突然,ABD :)
并没有发现任何解决方案。只有从MSDN无用的样本,不是工作。

Elad! Two years have passed and I faced the same problem. Suddenly, with ABD :) And found no solution. Only useless samples from MSDN that not works.

BeginOutputReadLine提出的解决方案()是一个很好的方式,但在的情况下,这样,它不适用,因为过程(当然,使用 WaitForExit( ))更早退出比异步输出完成彻底。

Proposed solutions with BeginOutputReadLine() is a good way but in situations such that, it is not applicable because process (certainly with using WaitForExit()) exits earlier than async output finished completely.

所以,我继续试图同步实施,并发现该解决方案是使用皮克()方法从的StreamReader 类。我加支票皮克()> -1 来确定它是不是流的作为的描述和终于它的工作原理,并停止悬挂的.aspx> MSDN文章!

So, I continue tried to implement it synchronously and found that the solution is in using Peek() method from StreamReader class. I added check for Peek() > -1 to sure that it is not the end of the stream as in MSDN article described and finally it works and stop hanging!

下面是code,希望它可以帮助人在那种情况下。

Here are the code, hope it helps somebody in situations like that.

var process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = @"C:\test\";
process.StartInfo.FileName = "test.exe";
process.StartInfo.Arguments = "your arguments here";

process.Start();
var output = new List<string>();

while (process.StandardOutput.Peek() > -1)
{
    output.Add(process.StandardOutput.ReadLine());
}

while (process.StandardError.Peek() > -1)
{
    output.Add(process.StandardError.ReadLine());
}
process.WaitForExit();

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

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