开发者

standardoutput pauses loop at end of stream C#

I am reading the output of a java application started using Process and reading stdError, stdOutout and using stdInput to send commands. Here is the relevant code:

        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.R开发者_如何学GoedirectStandardInput = 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() adds the line to a list of lines.

My problem is that the java app sometimes returns a string even when there is no input, so I always need to check for a new line. but If I need to send a command, and there is no new output, It will not send.

I tried different If statements on the readline, but it would only return the first few lines then it would stop.

basicly it seems to pause the loop if there is no new line for it to read.

How could I either setup my read/write loop differently or get the loop to unpause?

Thanks, Adam


try this:

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

and the little echoApp...

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


Please note if you try to access the output after CancelOutputRead() and CancelErrorRead() you may find that occasionally you are missing some text. I found the flush only occurs after explicitly calling Close(). Disposing (with a using statement) doesn't help.

This symptom will most likely occur when calling the command processor (CMD.EXE) because it does not explicitly flush itself. So be careful not to try and access the output (written from your event handlers) unless you explicitly call Close() first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜