Subject: Redirect both StandarOutput and StandardError
Posted By: jacob Post Date: 6/30/2005 9:56:04 PM
I have a problem which I can't seem to solve, even though I think I am on the right path. The problem is that, when I use the Process class I cannot redirect both StandardOutput and StandardError without freezing the application. I found the following...
quote:
[C#] 
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
In this case, if the child process writes any text to standard error it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.
... on the MSDN pages.

So it seems like there is a problem with this, and therefore I try to make two threads but I cannot make this work. This is how I have tried (the snippet is from the class that inherrits Process)...
...
this.Start();

OutputReader sor = new OutputReader();
sor.StreamReader = this.StandardOutput;
if(this.StandardOutputFile != null)
    stdoutthread = new Thread(new ThreadStart(sor.Start));

OutputReader ser = new OutputReader();
ser.StreamReader = this.StandardError;
if(this.StandardErrorFile != null)
    stderrthread = new Thread(new ThreadStart(ser.Start));

this.WaitForExit();
...
... and then the OutputReader...
class OutputReader
{
    public StreamReader StreamReader
    {
        set{ this.reader = value; }
    }
    private StreamReader reader = null;

    public string Reads
    {
        get{ return this.reads; }
    }
    private string reads;

    public void Start()
    {
        Console.WriteLine("reading...");
        reads = this.reader.ReadToEnd();        
    }
}
What do I do wrong? I am not too experienced in using Threads. The process which I do using the class is to checkout files from CVS, so it is quite a lot of output, and it has to be said that it freezes after it has checked out half of the project.

Thanks, Jacob.

Reply By: jacob Reply Date: 7/1/2005 5:28:39 PM
Big ups!

Such a big effort for an obvious thing... that is, late at night things seem to slip through! The solution I posted was almost correct... except from the fact that it lacks the Start call!

Guess I could have deleted the post and saved the embarrassment, but I guess others can benefit from the threaded redirection example.


Go to topic 32290

Return to index page 518
Return to index page 517
Return to index page 516
Return to index page 515
Return to index page 514
Return to index page 513
Return to index page 512
Return to index page 511
Return to index page 510
Return to index page 509