I am a newbie with Streams and I’m trying to write a function that redirects a Process’s StandardOutput and returns the StandardOutput Stream so that it can be consumed elsewhere. When I return the Process.StandardOutput Stream in the method, how is that Stream stored? Does it just live in memory somewhere until it is consumed? Does it have a maximum size?
Here’s some example code that I have so far:
public Stream GetStdOut(string file)
{
_process = new Process(file);
_process.StartInfo.UseShellExecute = false;
_process.StartInfo.RedirectStandardOutput = true;
_process.Start();
return _process.StandardOutput;
}
public bool CompareStreams()
{
Stream s1 = GetStdOut("somefile.exe");
Stream s2 = GetStdOut("anotherfile.exe");
using (StreamReader sr1 = new StreamReader(s1))
using (StreamReader sr2 = new StreamReader(s2))
{
string line_a, line_b;
while ((line_a = sr1.ReadLine()) != null &&
(line_b = sr2.ReadLine()) != null)
{
if (line_a != line_b)
return false;
}
return true;
}
}
So in CompareStreams(), do I need to be worried about how much data is associated with Stream s1 while I’m generating data for Stream s2? Or does this not really matter?
The Stream object does not immediately get filled with all of the standard out data.
As the processes run, they write to a buffer, and when the buffer is full, they will freeze until you read from the buffer.
As you read from the buffer, that clears space for the process to write more data out.