1

I am looking into stream reader timeout property. From the documentation, I did not understand,

  • How this property works?
  • What will happen when stream reader timeouts?

Can somebody explain me these questions or point out to some better documentation than this.

https://msdn.microsoft.com/en-us/library/system.io.stream.readtimeout(v=vs.110).aspx

Sample code:

TcpClient client = new TcpClient(serverIP, serverTcpPort);
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
sr.BaseStream.ReadTimeout = 100;
SHAHS
  • 462
  • 1
  • 5
  • 13
  • What aren't you grasping? The documentation tells you everything. If it times out, an exception is thrown. – ThePerplexedOne Jun 22 '17 at 08:12
  • 1
    The documentation says it throws InvalidOperationException if it is not supported, nothing is written what will happen when it timeouts, I had it tested in code, it behaves randomly – SHAHS Jun 22 '17 at 08:15
  • 1
    To understand documentation, you need to read the *correct* documentation. Which implementation of `Stream` is being used? `FileStream`? `MemoryStream`? Go to the documentation for *that* class, instead, and all will probably be revealed. – J. Steen Jun 22 '17 at 08:16
  • edited question to add sample code I have – SHAHS Jun 22 '17 at 08:25

1 Answers1

3

The documentation quite clearly says, that not every Stream implements ReadTimeout. Some subclasses of Stream may implement this property. So you need to check the documentation of subclasses to learn about the usage of ReadTimeout. Your code snippet would work with a NetworkStream, which is returned by

Stream s = client.GetStream();

The Microsoft website does have some specific documentation for this NetworkStream class and its ReadTimeout property, which you can find here: https://msdn.microsoft.com/en-us/library/bk6w7hs8(v=vs.110).aspx

Kai Weber
  • 388
  • 4
  • 14
  • I have added the sample code I am using, I have tried looking into different streams networkStream, IOStream, they all eventually point out to same document page. – SHAHS Jun 22 '17 at 08:28
  • I've updated my answer, pointing to the ReadTimeout property documentation of the NetworkStream class which you are using in your code. – Kai Weber Jun 22 '17 at 09:18
  • Thanks for the help :) – SHAHS Jun 22 '17 at 10:00