Building a StreamReader timeout RRS feed

  • Question

  • My ultimate goal is to have a read function that reads from StreamReader and you can pass a timeout. Once it has expired it returns whatever it received to that point.

    StreamReader.Readline, Peek, and EndOfStream all block when there is nothing in the pipe.

    I need to wrap a timeout around this.  I have looked at Stopwatch, Timers, and Async.  I figure someone has had to deal with this in the past and wrote something.  If you could please share I would be very thankful.


    Gory details:

    I am reading from a StreamReader that is attached to a redirected Process.StandardOutput.

    the Process is attached to a plink executable connected to a linux system.

    I am just executing simple commands but when it gets to where it is waiting for input again the read hangs.

    I have looked at SharpSSh which allows you to enter a prompt and this helps this but it fails me on other areas of my application.

    Thanks for your time!

    Thursday, July 28, 2011 1:31 PM

All replies

  • What is the underlying stream? You may be lucky and it supports timeout but you need to work directly with the stream instead of a reader.


    Thursday, July 28, 2011 2:02 PM
  • Here are the relevant lines.

    StreamReader plinkReader;
    plinkProc.StartInfo.UseShellExecute = false;
          plinkProc.StartInfo.CreateNoWindow = true;
          plinkProc.StartInfo.RedirectStandardInput = true;
          plinkProc.StartInfo.RedirectStandardOutput = true;
          plinkProc.StartInfo.RedirectStandardError = true;
    plinkReader = plinkProc.StandardOutput;

    so the underlying reader is the standard StreamReader class.

    Does that answer your question?  I did see any timeout reader available for this class

    Thursday, July 28, 2011 3:05 PM
  • You can get to the underlying stream via BaseStream property of the reader.
    Thursday, July 28, 2011 3:18 PM
  • It says System.IO.FileStream

    Under the timeouts on that stream it says time out are not supported.

    Thursday, July 28, 2011 4:10 PM
  • You may have to do async reading from the base stream to achieve what you seem to want.
    Thursday, July 28, 2011 6:51 PM