Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(updated 12/24/2012)
So far this is my code after solution 1 and 2 were helpfull by didint intirly work for me
C#
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
Updated 24-Dec-12 8:49am
v3

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:
C#
ServerProcess.OutputDataReceived += new DataReceivedEventHandler(ServerProcess_OutputDataReceived);

and handler should be like:
C#
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.
 
Share this answer
 
v2
Comments
Yuki24 24-Dec-12 14:56pm    
much of the code you showed helped but still didint do exactly what i hoped for but thank you
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.
 
Share this answer
 
Comments
Yuki24 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 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 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 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900