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.