First of all, not having anything useful to answer is not an excuse for being
offensive. If you read the Remarks sections in the VS80 documentation for the
StreamReader.Read(char[], int, int) and the StreamReader.ReadBlock() methods
it is only reasonable to come to the conclusion that this overload of Read()
is a non blocking version of ReadBlock(). One would wonder otherwise what the
purpose of ReadBlock could be.
By this I mean that it is reasonable to expect that Read(char[], int, int)
return immediately when there are no characters available to be read; instead
it blocks.
Here's a scaled down excerpt from my code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
namespace StreamReaderTest
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
proc.StandardInput.WriteLine("dir");
string outLine = proc.StandardOutput.ReadLine();
char[] buffer = new char[4096];
proc.StandardError.Read(buffer, 0, 4096);
}
}
}
I need to replace the last two lines of code with a way to access the
program's standard error if there's any and not block if there isn't.
Cheers,
Nicola Musatti
Peter Duniho said:
Hallo,
Is there any way to perform a non blocking read from a child process's
standard error? I tried using the StandardError property of the
System.Diagnostics.Process class without success. According to the
documentation the StreamReader.Read(char[], int, int) function should not
block, but it does. Is there any alternative?
Please post a concise-but-complete code sample showing what you've tried,
and explaining what about it doesn't work as you expect.
You should be able to use the StandardError property to redirect stderr
from the process. But to do so successfully means reading the
documentation and understanding what you have to do. You need to set the
appropriate "Redirect..." property, and you need to understand that the
StreamReader.Read() method _always_ blocks until there is something to
read (so whatever you read that you _think_ says it should not block, you
misread).
If you post code to demonstrate what you're trying to do, it should be
possible to discuss and answer any issues you're having with that code.
Otherwise, not so much.
Pete