Unfortunately Bueller's hint did not work for me, because ReadLine
can block.
But with Zach's answer on Alternative to StreamReader.Peek and Thread.Interrupt I came up with the following:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool PeekNamedPipe(SafeHandle handle,
byte[] buffer, uint nBufferSize, ref uint bytesRead,
ref uint bytesAvail, ref uint BytesLeftThisMessage);
static bool SomethingToRead(SafeHandle streamHandle)
{
byte[] aPeekBuffer = new byte[1];
uint aPeekedBytes = 0;
uint aAvailBytes = 0;
uint aLeftBytes = 0;
bool aPeekedSuccess = PeekNamedPipe(
streamHandle,
aPeekBuffer, 1,
ref aPeekedBytes, ref aAvailBytes, ref aLeftBytes);
if (aPeekedSuccess && aPeekBuffer[0] != 0)
return true;
else
return false;
}
In my case the additional P/Invoke call is no problem.