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.
|