Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
(updated 12/24/2012)
So far this is my code after solution 1 and 2 were helpfull by didint intirly work for me
private void button1_Click(object sender, EventArgs e)
        { 
            {
                Process ServerProcess = new Process();
                ServerProcess.StartInfo.FileName = "CMD.exe";
                ServerProcess.StartInfo.Arguments = "/c java -jar Server.jar";
                ServerProcess.StartInfo.CreateNoWindow = true;
                ServerProcess.StartInfo.UseShellExecute = false;
                ServerProcess.StartInfo.RedirectStandardError = true;
                ServerProcess.StartInfo.RedirectStandardOutput = false;
                ServerProcess.StartInfo.RedirectStandardInput = true;
                ServerProcess.Start();
                ServerProcess.BeginErrorReadLine();
                ServerProcess.WaitForExit();
                ServerProcess.StartInfo.RedirectStandardError = true;
 
                ServerProcess.ErrorDataReceived += new DataReceivedEventHandler(ServerProcess_ErrorDataReceived);
            }  
        } 
        void ServerProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
 
        }
(First Post info)
I have been having troubles trying to find out how to corectly redirect the output and input.
the form will have a richtextbox1 for the output and a textbox1 to send input to the console and the button1 to start the server. this is a minecraft server btw.
so i want the output to creat new lines for every thing that comes out so i can see whats going on with the server.
if anyone knows how I could go abotu this I would be greatfull.
(New Info)
the program does use error not output so i want this to be redirected to richTextBox1 in realtime all i have gotton so far was one line with soem different code but i want it to fill new lines for every entry and auto scroll
Posted 23-Dec-12 20:44pm
Yuki24238
Edited 24-Dec-12 8:49am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is also the StandardError you may want to redirect - some applications use it as well.
 
There is an article showing to do deal with all three here: How to redirect Standard Input/Output of an application[^] which may be of help.
  Permalink  
Comments
Yuki24 at 24-Dec-12 14:48pm
   
Helped me realize it used Error not output but the link didint really help me redirect the info to the richTextBox1
OriginalGriff at 24-Dec-12 15:02pm
   
So? Is this a major problem? You know how to get teh text now, so it is a trivial task to set a Text Property, isn't it? :laugh:
Yuki24 at 24-Dec-12 15:23pm
   
my problem is i dont know how to make it autoscrool and relay the info in new lines instead of a single changign line the program you sent me didint really work i tried the code nothign jsut locks up
OriginalGriff at 25-Dec-12 5:12am
   
Setting the text property on a new line is really not difficult...
myRichTextBox.Text += "\n" + myTextBox.Text;
And auto scroll is the RichTextBox default.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There are two approaches.
First uses events for async reading.
In addition to your code:
ServerProcess.BeginOutputReadLine();
you'll need to first attach event handlers:
ServerProcess.OutputDataReceived += new DataReceivedEventHandler(ServerProcess_OutputDataReceived);
and handler should be like:
void ServerProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    if (!string.IsNullOrEmpty(e.Data))
    {
        UpdateTextBox(e.Data);
    }
}
This works well when process outputs text data with line feeds (Console.Out.WriteLine()) and flushes output stream regulary (Console.Out.Flush()).
For each WriteLine your handler will be called.
If process uses only Write() your handler will not be called until output stream is closed and you'll get all data at once.
 
Second approach is not to use BeginOutputReadLine() but setup your own thread or BackgroungWorker to read data asynchronously as it comes. It's a bit harder to setup, but you don't depend on line feeds and you can even use it to transfer binary data.
  Permalink  
v2
Comments
Yuki24 at 24-Dec-12 14:56pm
   
much of the code you showed helped but still didint do exactly what i hoped for but thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 355
1 _Amy 235
2 Peter Leow 185
3 Andreas Gieriet 180
4 Dave Kreskowiak 155
0 OriginalGriff 7,540
1 Sergey Alexandrovich Kryukov 6,462
2 Maciej Los 3,849
3 Peter Leow 3,653
4 CHill60 2,712


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100