-
September 19th, 2011, 11:28 AM
#1
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;
}
-
September 21st, 2011, 03:22 AM
#2
-
September 21st, 2011, 07:38 PM
#3
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.
-
September 22nd, 2011, 06:17 AM
#4
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.
-
September 23rd, 2011, 08:08 AM
#5
Re: stdout live
Added the WaitForExit().
No change, still have to wait for the program to exit
-
September 23rd, 2011, 08:50 AM
#6
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.
-
September 24th, 2011, 10:06 AM
#7
Re: stdout live
Originally Posted by DataMiser
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|