GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2009, 23:43
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

C# - check stdin for piped data without blocking


How about this one.
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 of several things I/ve tried:
C-SHARP / C# 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 still blocks the same.:

    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;
  }
}
It's like the stream is dead until a character arrives.
After ENTER is pressed the -1 is appended after it.
Thanks
Last edited by Howard_L : 19-Oct-2009 at 00:50.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory leak when nothing is happening... How can I even debug this ? Algar MS Visual C++ / MFC Forum 10 19-Nov-2007 08:17
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 09:09
[GIM] Data Module - Contact dsmith C Programming Language 2 27-Jan-2005 17:30
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:42.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.