CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: stdout live

  1. #1
    Join Date
    Sep 2009
    Posts
    126

    stdout live

    Hi.

    I need help with two things:

    1) I'm getting the input from a JAR program, the minecraft server software to be exact.
    The code I'm using is working great, for 2 lines of text.
    After that I'm getting nothing.
    These two lines are information about the game (how many achievements and recipes).
    So when I run the program I get this info:
    148 recipes
    16 achievements
    which is correct, but only a part of all the text.

    2) I only get the text when I close the server program, its not "live".
    So I first run my program, which runs the server software, I close the server software and I get the stdout.
    I wan't stdout live!

    Here is my code:
    Code:
    private void button2_Click(object sender, EventArgs e)
            {
                string line;
                ProcessStartInfo processStartInfo = new ProcessStartInfo("java.exe", "-Xms512M -Xmx1024M -jar minecraft_server.jar");
                processStartInfo.UseShellExecute = false;
                processStartInfo.ErrorDialog = false;
                processStartInfo.RedirectStandardError = true;
                processStartInfo.RedirectStandardInput = true;
                processStartInfo.RedirectStandardOutput = true;
                Process process = new Process();
                process.StartInfo = processStartInfo;
                bool processStarted = process.Start();
                StreamWriter inputWriter = process.StandardInput;
                StreamReader outputReader = process.StandardOutput;
                while (!outputReader.EndOfStream)
                {
                    line = outputReader.ReadLine();
                    AddToLog(line);
                }
                StreamReader errorReader = process.StandardError;
            }
    
    private void AddToLog(string text)
            {
                textBox1.Text = textBox1.Text + Environment.NewLine + text;
            }

  2. #2
    Join Date
    Sep 2009
    Posts
    126

    Re: stdout live

    bump

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: stdout live

    You are not waiting for the process to end before collecting output (correctly), but if you do want all of the output you need a WaitForExit call or your code will run, exit, and stop reading. From the docs:

    Code:
    // Start the child process.
     Process p = new Process();
     // Redirect the output stream of the child process.
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.FileName = "Write500Lines.exe";
     p.Start();
     // Do not wait for the child process to exit before
     // reading to the end of its redirected stream.
     // p.WaitForExit();
     // Read the output stream first and then wait.
     string output = p.StandardOutput.ReadToEnd();
     p.WaitForExit();
    That said, I didn't have time to test this, so I may be wrong. Sorry in advance if I am.
    Last edited by BigEd781; September 21st, 2011 at 07:42 PM.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: stdout live

    That may not work. If you don't constantly read from StdOut it's possible the buffer will fill up and the application will just hang until something starts clearing the stdout buffer by reading from it.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Sep 2009
    Posts
    126

    Re: stdout live

    Added the WaitForExit().
    No change, still have to wait for the program to exit

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: stdout live

    I think there was some confusion about what you wanted. Sounds like you are looking to get real time updates while the program is running. I personally have no idea how you would do this as I have never looked into it.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Sep 2009
    Posts
    126

    Re: stdout live

    Quote Originally Posted by DataMiser View Post
    I think there was some confusion about what you wanted. Sounds like you are looking to get real time updates while the program is running. I personally have no idea how you would do this as I have never looked into it.
    Yes, thats what I want

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured