standardoutput在流C#结尾处暂停循环

时间:2011-05-06 22:02:30

标签: c# loops streamreader redirectstandardoutput

我正在阅读使用Process启动并读取stdError,stdOutout并使用stdInput发送命令的java应用程序的输出。以下是相关代码:

        int mem = Properties.Settings.Default.mem_max;
        string locale = Properties.Settings.Default.location;
        Process bukkit_jva = new Process();
        bukkit_jva.StartInfo.FileName = "java";
        //bukkit_jva.StartInfo.Arguments = "-Xmx" + mem + "M -Xms" + mem + "M -jar " + locale + "bukkit.jar";
        bukkit_jva.StartInfo.Arguments = "-Xmx512M -Xms512M -jar C:\\bukkit\\bukkit.jar";
        bukkit_jva.StartInfo.UseShellExecute = false;
        bukkit_jva.StartInfo.CreateNoWindow = true;
        bukkit_jva.StartInfo.RedirectStandardError = true;
        bukkit_jva.StartInfo.RedirectStandardOutput = true;
        bukkit_jva.StartInfo.RedirectStandardInput = true;
        bukkit_jva.Start();
        //start reading output
        SetText(bukkit_jva.StandardOutput.ReadLine());
        SetText(bukkit_jva.StandardOutput.ReadLine());
        SetText(bukkit_jva.StandardOutput.ReadLine());
        SetText(bukkit_jva.StandardOutput.ReadLine());
        StreamReader err = bukkit_jva.StandardError;
        StreamReader output = bukkit_jva.StandardOutput;
        StreamWriter writer = bukkit_jva.StandardInput;
        SetText(err.Peek().ToString());
        while (false == false)
        {
            if (vars.input != null)
            {
                writer.WriteLine(vars.input);
                vars.input = null;
            }
            SetText(output.ReadLine() + err.ReadLine());
        }
    }

SetText()将该行添加到行列表中。

我的问题是,即使没有输入,java应用程序有时会返回一个字符串,所以我总是需要检查一个新行。但如果我需要发送命令,并且没有新的输出,它将不会发送。

我在readline上尝试了不同的If语句,但它只会返回前几行然后才会停止。

基本上,如果没有可以读取的新行,它似乎会暂停循环。

我怎样才能设置不同的读/写循环或让循环取消暂停?

谢谢, 亚当

2 个答案:

答案 0 :(得分:0)

试试这个:

static void Main(string[] args)
{
    ProcessStartInfo psi = new ProcessStartInfo("echoApp.exe");
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;
    psi.UseShellExecute = false;

    Process echoApp = new Process();
    echoApp.ErrorDataReceived += new DataReceivedEventHandler(echoApp_ErrorDataReceived);
    echoApp.OutputDataReceived += new DataReceivedEventHandler(echoApp_OutputDataReceived);
    echoApp.StartInfo = psi;
    echoApp.Start();
    echoApp.BeginOutputReadLine();
    echoApp.BeginErrorReadLine();
    echoApp.StandardInput.AutoFlush = true;

    string str = "";
    while (str != "end")
    {
        str = Console.ReadLine();
        echoApp.StandardInput.WriteLine(str);
    }
    echoApp.CancelOutputRead();
    echoApp.CancelErrorRead();
    echoApp.Close();
}

static void echoApp_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine("stdout: {0}", e.Data);
}

static void echoApp_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine("stderr: {0}", e.Data);
}

和小echoApp ......

//echoApp
static void Main(string[] args)
{
    string str="";
    while (str != "end")
    {
        str = Console.ReadLine();
        Console.WriteLine(str);
    }
}

答案 1 :(得分:0)

请注意,如果您尝试在CancelOutputRead()和CancelErrorRead()之后访问输出,您可能会发现偶尔会丢失一些文本。我发现刷新仅在显式调用Close()之后发生。处置(使用using语句)没有帮助。

调用命令处理器(CMD.EXE)时很可能会出现此症状,因为它没有显式刷新自身。因此,除非先显式调用Close(),否则请注意不要尝试访问输出(从事件处理程序中写入)。