StreamReader.ReadToEnd and indefinite blocking

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nad

    StreamReader.ReadToEnd and indefinite blocking

    Hello All,

    I have been trying to get some sort of documantion on how
    StreamReader.Re adToEnd() 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.Execu te(cp);
    StreamReader sr = new StreamReader(st ream);
    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.
  • Jon Skeet [C# MVP]

    #2
    Re: StreamReader.Re adToEnd and indefinite blocking

    Nad <nadeem_far@yah oo.com> wrote:[color=blue]
    > I have been trying to get some sort of documantion on how
    > StreamReader.Re adToEnd() 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.Execu te(cp);
    > StreamReader sr = new StreamReader(st ream);
    > 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().[/color]

    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.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Nad

      #3
      Re: StreamReader.Re adToEnd and indefinite blocking

      Thanks For the help Jon.


      Nadeem

      Jon Skeet [C# MVP] <skeet@pobox.co m> wrote in message news:<MPG.1bc5c 4ec36320ebb98b5 50@msnews.micro soft.com>...[color=blue]
      > Nad <nadeem_far@yah oo.com> wrote:[color=green]
      > > I have been trying to get some sort of documantion on how
      > > StreamReader.Re adToEnd() 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.Execu te(cp);
      > > StreamReader sr = new StreamReader(st ream);
      > > 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().[/color]
      >
      > 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.[/color]

      Comment

      Working...