StreamReader.ReadToEnd and indefinite blocking

  • Thread starter Thread starter Nad
  • Start date Start date
N

Nad

Hello All,

I have been trying to get some sort of documantion on how
StreamReader.ReadToEnd() finds out if the stream has ended and if
anyone can read the following lines and tell me how to control
indefinite blocking issue. these are from MSDN.

"ReadToEnd assumes that the stream knows when it has reached an end.
For interactive protocols, in which the server sends data only when
you ask for it and does not close the connection, ReadToEnd might
block indefinitely and should be avoided."


I have code that reads from the NetworkStream and loads it into the
string builder. here is the code

NetworkStream stream = (NetworkStream)connector.Execute(cp);
StreamReader sr = new StreamReader(stream);
StringBuilder sbuilder = new StringBuilder();
sbuilder.Append( sr.ReadToEnd());

Now, the code will freeze if the response is not complete. How can I
use this code and what changes I will need to make to avoid that
indefinite blocking issue with ReadToEnd() .


help is appriciated.
 
Nad said:
I have been trying to get some sort of documantion on how
StreamReader.ReadToEnd() finds out if the stream has ended and if
anyone can read the following lines and tell me how to control
indefinite blocking issue. these are from MSDN.

"ReadToEnd assumes that the stream knows when it has reached an end.
For interactive protocols, in which the server sends data only when
you ask for it and does not close the connection, ReadToEnd might
block indefinitely and should be avoided."


I have code that reads from the NetworkStream and loads it into the
string builder. here is the code

NetworkStream stream = (NetworkStream)connector.Execute(cp);
StreamReader sr = new StreamReader(stream);
StringBuilder sbuilder = new StringBuilder();
sbuilder.Append( sr.ReadToEnd());

Now, the code will freeze if the response is not complete. How can I
use this code and what changes I will need to make to avoid that
indefinite blocking issue with ReadToEnd().

You can't, basically. NetworkStreams are never going to know that
they've ended until they've been closed. You can close the stream from
another thread after a timeout if you want, which I believe should
work, but you can't get ReadToEnd to time out itself, as far as I know.
 
Thanks For the help Jon.


Nadeem

Jon Skeet said:
You can't, basically. NetworkStreams are never going to know that
they've ended until they've been closed. You can close the stream from
another thread after a timeout if you want, which I believe should
work, but you can't get ReadToEnd to time out itself, as far as I know.
 
Back
Top