check stdin for piped data without blocking
Hi,
I want to detect if there is piped data waiting in stdin when my prog starts.
But it seems that stdin does not even contain an EOF until there is input.
As a result I get a block... Example:
Code:
using System;
using System.Text; // for ReadKey()
using System.IO; // for StreamReader etc
class MyProg
{
static int Main(string[] args)
{
//string s1;
//int i;
char c;
Console.WriteLine("-----test-----");
/**** So far everythin has blocked the same as this: ****/
while( Console.In.Peek() != -1 ) // blocks at first read...
{
//s1 = Console.ReadLine();
//Console.Write(s1);
//i = Console.Read();
//Console.Write("{0} ", i);
c = (char)Console.Read();
Console.Write("{0} ", c);
//c = Console.ReadKey(); // even with: using System.Text; still gets:
// Error: System.Console' does not contain a definition for `ReadKey'
//Console.Write("{0} ", c);
}
/**** This blocks...
TextReader tIn = Console.In;
while( tIn.Peek() != -1 )
{
c = (char)tIn.Read();
Console.Write("{0} ", c );
}
****/
/**** This works to read stdin but blocks the same as regular stdin:
StreamReader sin = new StreamReader(Console.OpenStandardInput());
while( sin.Peek() != -1 )
{
c = (char)sin.Read();
Console.Write("{0} ", c);
if(c == 'x') break;
}
sin.Close();
#### Sample run:
-> mcs peek_stdio.cs
-> mono peek_stdio.exe
-----test-----
testxinput
t e s t x ->
****/
return 0;
}
}
I'm still looking for somethig at msdn (on dialaup= drag)
Something like <conio.h>'s getch() I guess...