# Solved: C# Asynchronous Process StandardOutput Read?



## avisitor

Does anyone know how to read asynchronously from the StandardOutput of a process started programatically in C#. I'm able to read from it now, but it appears only after the program has finished executing. Sometimes the execution can take 15-20 seconds which leaves the user waiting with no response.

I've tried:



Code:


            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.FileName = file;
            process.StartInfo.Arguments = arg;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            String output = "";
            while (process.HasExited == false)
            {
                output += process.StandardOutput.ReadLine() + "\r\n";
            }
            process.WaitForExit();

and



Code:


            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.FileName = file;
            process.StartInfo.Arguments = arg;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            String output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

With neither doing what I want. The code is executed by a separate thread from the GUI, so the GUI doesn't hang and keeps the user (falsely) informed by moving a progress bar every couple seconds.  However, I'd much rather do an async read, so the results are displayed as soon as they become available.


----------



## avisitor

No one on this? Ah, well, whatever. I have an idea, but I'm not totally sure.


----------



## IMM

Did the stuff in the links at http://forums.techguy.org/software-development/736773-simple-network-program-2.html not provide any help?
(this is from the 3rd one)


Code:


// Set UseShellExecute to false for redirection.
            sortProcess.StartInfo.UseShellExecute = false;

            // Redirect the standard output of the sort command.  
            // This stream is read asynchronously using an event handler.
            sortProcess.StartInfo.RedirectStandardOutput = true;
            sortOutput = new StringBuilder("");

            // Set our event handler to asynchronously read the sort output.
            sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

I thought the key to this was setting _UseShellExecute = false_ and never mixing synchronous and async methods to do so -- but I'm not really a C# type

One thing I recall about some of the MS network proggies (forgotten which ones) is that they will throw to both stderr and stdout - so you may need to grab both.

The other thing I wonder about is _\r\n_ -- perhaps it only throws newline?


----------



## volatile

The MSDN is a great reference for these types of questions. To answer your question; Read, ReadLine, and ReadToEnd are apparently synchronous operations. To read asynchronously use BeginOutputReadLine.

Definitely read this:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

Pay close attention to the sections describing how to avoid dead locks.


----------



## avisitor

Yeah, I found it. Thanks though. Marking this solved.


----------

