问题
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;
回答1:
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
来源:https://stackoverflow.com/questions/44693705/c-sharp-stream-readtimeout-property