Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2005, 09:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Redirect both StandarOutput and StandardError

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:
quote:
Code:
[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)...
Code:
...
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...
Code:
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.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old July 1st, 2005, 05:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect SKhna ASP.NET 2.0 Basics 1 February 9th, 2008 08:14 AM
response.redirect sarah lee ASP.NET 1.0 and 1.1 Basics 1 October 27th, 2006 08:57 AM
response.redirect ava_h .NET Framework 2.0 0 October 18th, 2006 10:21 PM
Redirect Problems faithfulman ASP.NET 2.0 Basics 11 September 17th, 2006 04:56 AM
URL Redirect Help david29118 Classic ASP Basics 3 April 3rd, 2005 06:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.