Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stream Reader

Stream Reader

Scheduled Pinned Locked Moved C#
comperformance
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Frank Kerrigan
    wrote on last edited by
    #1

    This does not appear to be working and I can't figure out why MemoryStream ms = new MemoryStream(); string message; ….some code here to fill the memory stream…. Using (StreamReader sr = new StreamReader(ms)) { message = sr.ReadToEnd(); } ms.Position = 0;


    Blog Have I http:\\www.frankkerrigan.com

    C J 2 Replies Last reply
    0
    • F Frank Kerrigan

      This does not appear to be working and I can't figure out why MemoryStream ms = new MemoryStream(); string message; ….some code here to fill the memory stream…. Using (StreamReader sr = new StreamReader(ms)) { message = sr.ReadToEnd(); } ms.Position = 0;


      Blog Have I http:\\www.frankkerrigan.com

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Frank Kerrigan wrote:

      This does not appear to be working and I can't figure out why

      What exactly do you mean by "not working"?


      *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

      F 2 Replies Last reply
      0
      • C Colin Angus Mackay

        Frank Kerrigan wrote:

        This does not appear to be working and I can't figure out why

        What exactly do you mean by "not working"?


        *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

        F Offline
        F Offline
        Frank Kerrigan
        wrote on last edited by
        #3

        This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.


        Blog Have I http:\\www.frankkerrigan.com

        A C 2 Replies Last reply
        0
        • F Frank Kerrigan

          This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.


          Blog Have I http:\\www.frankkerrigan.com

          A Offline
          A Offline
          Alois Kraus
          wrote on last edited by
          #4

          This is a lifetime issue because the underlying stream has been finalized before the StreamReader was flushed. This article should help you to get more light into this issue: http://geekswithblogs.net/akraus1/articles/81629.aspx Yours, Alois Kraus

          C 1 Reply Last reply
          0
          • F Frank Kerrigan

            This code was taken from a live web service that had been running perfectly happily for nearly 3 months and then all of a sudden stopped working, throwing the ObjectDisposedException.


            Blog Have I http:\\www.frankkerrigan.com

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            The reason is because the using block closes the StreamReader when you move out of the scope of the using block. When you close a StreamReader OR a StreamWriter, however, the underlying stream object is also closed and thus disposed. If your stream was a FileStream then that might make logical sense but it is not so obvious with a MemoryStream because you probably want to use it as temporary storage or pass to another method for further processing etc. Even if you did not use the using block the MemoryStream would still be closed the moment the StreamReader is disposed of or garbage collected. I've no idea why it would just suddenly show up now after so many months - That is a curiosity.


            *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              Frank Kerrigan wrote:

              This does not appear to be working and I can't figure out why

              What exactly do you mean by "not working"?


              *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

              F Offline
              F Offline
              Frank Kerrigan
              wrote on last edited by
              #6

              Thanks guys


              Blog Have I http:\\www.frankkerrigan.com

              1 Reply Last reply
              0
              • A Alois Kraus

                This is a lifetime issue because the underlying stream has been finalized before the StreamReader was flushed. This article should help you to get more light into this issue: http://geekswithblogs.net/akraus1/articles/81629.aspx Yours, Alois Kraus

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                Alois Kraus wrote:

                http://geekswithblogs.net/akraus1/articles/81629.aspx

                Sorry, that link doesn't work for me.


                *** Developer Day 4 in Reading, England on 2nd December 2006 - Registration Now Open *** Upcoming Scottish Developers events: * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog | Photos

                1 Reply Last reply
                0
                • F Frank Kerrigan

                  This does not appear to be working and I can't figure out why MemoryStream ms = new MemoryStream(); string message; ….some code here to fill the memory stream…. Using (StreamReader sr = new StreamReader(ms)) { message = sr.ReadToEnd(); } ms.Position = 0;


                  Blog Have I http:\\www.frankkerrigan.com

                  J Offline
                  J Offline
                  Judah Gabriel Himango
                  wrote on last edited by
                  #8

                  sr.ReadToEnd will read to the end from the current position. Make sure the position is 0 before reading to end. Also, don't touch the memory stream after it has been disposed. The using(...) block will dispose the object; it will actually compile down to this:

                  StreamReader sr = null;
                  try
                  {
                  sr = new StreamReader(ms);
                  message = sr.ReadToEnd();
                  }
                  finally
                  {
                  if(sr != null)
                  {
                  sr.Dispose();
                  }
                  }

                  Since it gets disposed when the using block leaves scope, callign ms.Position = 0 after the using scope is going to cause problems.

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups