There are two approaches.
First uses events for async reading.
In addition to your code:
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)
This works well when process outputs text data with line feeds (
) and flushes output stream regulary (
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
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.