A normal Stream might provide a timeout on a specific read opreation. However, using StreamReader.ReadToEnd() on it can still get stuck for a long time. This class offers a timeout from the moment of it's construction to the read. Every read past the timeout from the stream's construction will fail. If the timeout elapsed while a read is in progress TimeoutStream is not responsible for aborting the read (there is no known good way in .NET to do it) See - http://www.dotnet247.com/247reference/msgs/36/182553.aspx and
- http://www.google.co.il/search?q=cancel+async+Stream+read+.net
Stream originalStream = GetStream(); StreamReader reader = new StreamReader(new TimeoutStream(originalStream, 5000)); // assuming the originalStream has a per-operation timeout, then ReadToEnd() // will return in (5000 + THAT_TIMEOUT) string foo = reader.ReadToEnd();