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. Difference between flush\dispose\close

Difference between flush\dispose\close

Scheduled Pinned Locked Moved C#
question
16 Posts 7 Posters 0 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.
  • S Offline
    S Offline
    sharpiesharpie
    wrote on last edited by
    #1

    In all my applications, every time i used something like a FileStream, when i finished using it i called: fs.Flush(); fs.Dispose(); fs.Close(); I'm wondering, what's the difference between flushing and disposing or just closing? what's the most efficient way of doing it?

    A S C S 4 Replies Last reply
    0
    • S sharpiesharpie

      In all my applications, every time i used something like a FileStream, when i finished using it i called: fs.Flush(); fs.Dispose(); fs.Close(); I'm wondering, what's the difference between flushing and disposing or just closing? what's the most efficient way of doing it?

      A Offline
      A Offline
      Abisodun
      wrote on last edited by
      #2

      Check this out: http://msdn2.microsoft.com/en-us/library/system.io.filestream_members.aspx[^]

      1 Reply Last reply
      0
      • S sharpiesharpie

        In all my applications, every time i used something like a FileStream, when i finished using it i called: fs.Flush(); fs.Dispose(); fs.Close(); I'm wondering, what's the difference between flushing and disposing or just closing? what's the most efficient way of doing it?

        S Offline
        S Offline
        Sandeep Akhare
        wrote on last edited by
        #3

        OK FileStream reads the content in bytes fs.Flush()-> method clears the buffer which is used by fs while reading or writing fs.Dispose()-> make the object ready for Garbage collection no instance in Memory fs.close()-> Only close the fileStream without doing anything

        Thanks and Regards Sandeep

        S 1 Reply Last reply
        0
        • S Sandeep Akhare

          OK FileStream reads the content in bytes fs.Flush()-> method clears the buffer which is used by fs while reading or writing fs.Dispose()-> make the object ready for Garbage collection no instance in Memory fs.close()-> Only close the fileStream without doing anything

          Thanks and Regards Sandeep

          S Offline
          S Offline
          sharpiesharpie
          wrote on last edited by
          #4

          well, then, i can use dispose instead of flush and close? or flush and dispose without close? o_O

          S 1 Reply Last reply
          0
          • S sharpiesharpie

            well, then, i can use dispose instead of flush and close? or flush and dispose without close? o_O

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #5

            Its good practice to flush the buffer before closing the stream after closing the you can call dispose method . while writing if you forget to call close then it will not save the file which you are manupilating

            Thanks and Regards Sandeep

            S 1 Reply Last reply
            0
            • S Sandeep Akhare

              Its good practice to flush the buffer before closing the stream after closing the you can call dispose method . while writing if you forget to call close then it will not save the file which you are manupilating

              Thanks and Regards Sandeep

              S Offline
              S Offline
              sharpiesharpie
              wrote on last edited by
              #6

              no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)

              S G L 3 Replies Last reply
              0
              • S sharpiesharpie

                no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)

                S Offline
                S Offline
                Sandeep Akhare
                wrote on last edited by
                #7

                Sorry you are write Calling Dispose allows the resources used by the Stream to be reallocated for other purposes. Flushing the stream will not flush its underlying encoder unless you explicitly call an implementation of Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream Taken from MSDN :)

                Thanks and Regards Sandeep

                1 Reply Last reply
                0
                • S sharpiesharpie

                  In all my applications, every time i used something like a FileStream, when i finished using it i called: fs.Flush(); fs.Dispose(); fs.Close(); I'm wondering, what's the difference between flushing and disposing or just closing? what's the most efficient way of doing it?

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

                  sharpiesharpie wrote:

                  fs.Dispose(); fs.Close();

                  Close and Dispose in this class are synonyms of each other - they do the same thing. As implemented, Close() calls Dispose() which, in turn, calls Flush() Flush writes out anything remaining in the buffer to the file.


                  Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

                  S 1 Reply Last reply
                  0
                  • S sharpiesharpie

                    no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    sharpiesharpie wrote:

                    if you dispose after you close it throws an exception

                    If it does, it's a bug in the implementation. You should be able to call Dispose any number of times, and if the object already has been disposed, it should ignore it.

                    --- single minded; short sighted; long gone;

                    1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      sharpiesharpie wrote:

                      fs.Dispose(); fs.Close();

                      Close and Dispose in this class are synonyms of each other - they do the same thing. As implemented, Close() calls Dispose() which, in turn, calls Flush() Flush writes out anything remaining in the buffer to the file.


                      Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

                      S Offline
                      S Offline
                      Scott Dorman
                      wrote on last edited by
                      #10

                      Colin Angus Mackay wrote:

                      As implemented, Close() calls Dispose() which, in turn, calls Flush()

                      Actually, they don't call Flush. I just double-checed that through Reflector. The call order is backwards as well: Dispose does call Close, which then calls a protected Dispose(bool) method which actually does all of the work.

                      ----------------------------- In just two days, tomorrow will be yesterday.

                      C 1 Reply Last reply
                      0
                      • S sharpiesharpie

                        In all my applications, every time i used something like a FileStream, when i finished using it i called: fs.Flush(); fs.Dispose(); fs.Close(); I'm wondering, what's the difference between flushing and disposing or just closing? what's the most efficient way of doing it?

                        S Offline
                        S Offline
                        Scott Dorman
                        wrote on last edited by
                        #11

                        Dispose and Close are equivalent, so you can call either one (or both). Flush will write the contents in memory to the file (the stream's backing store).

                        ----------------------------- In just two days, tomorrow will be yesterday.

                        1 Reply Last reply
                        0
                        • S Scott Dorman

                          Colin Angus Mackay wrote:

                          As implemented, Close() calls Dispose() which, in turn, calls Flush()

                          Actually, they don't call Flush. I just double-checed that through Reflector. The call order is backwards as well: Dispose does call Close, which then calls a protected Dispose(bool) method which actually does all of the work.

                          ----------------------------- In just two days, tomorrow will be yesterday.

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

                          Scott Dorman wrote:

                          I just double-checed that through Reflector

                          Ummm... I got my information through reflector. Dispose calls close on an object of a class called __HandleProtector, not FileStream.

                          public override void Close()
                          {
                          this.Dispose(true);
                          GC.nativeSuppressFinalize(this);
                          }

                          protected virtual void Dispose(bool disposing)
                          {
                          if (this._handleProtector != null)
                          {
                          if (!this._handleProtector.IsClosed)
                          {
                          this.Flush();
                          }
                          this._handleProtector.Close();
                          }
                          this._canRead = false;
                          this._canWrite = false;
                          this._canSeek = false;
                          this._buffer = null;
                          }

                          public override void Flush()
                          {
                          if (this._handleProtector.IsClosed)
                          {
                          __Error.FileNotOpen();
                          }
                          if (this._writePos > 0)
                          {
                          this.FlushWrite();
                          }
                          else if ((this._readPos < this._readLen) && this.CanSeek)
                          {
                          this.FlushRead();
                          }
                          }


                          Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

                          S S 2 Replies Last reply
                          0
                          • C Colin Angus Mackay

                            Scott Dorman wrote:

                            I just double-checed that through Reflector

                            Ummm... I got my information through reflector. Dispose calls close on an object of a class called __HandleProtector, not FileStream.

                            public override void Close()
                            {
                            this.Dispose(true);
                            GC.nativeSuppressFinalize(this);
                            }

                            protected virtual void Dispose(bool disposing)
                            {
                            if (this._handleProtector != null)
                            {
                            if (!this._handleProtector.IsClosed)
                            {
                            this.Flush();
                            }
                            this._handleProtector.Close();
                            }
                            this._canRead = false;
                            this._canWrite = false;
                            this._canSeek = false;
                            this._buffer = null;
                            }

                            public override void Flush()
                            {
                            if (this._handleProtector.IsClosed)
                            {
                            __Error.FileNotOpen();
                            }
                            if (this._writePos > 0)
                            {
                            this.FlushWrite();
                            }
                            else if ((this._readPos < this._readLen) && this.CanSeek)
                            {
                            this.FlushRead();
                            }
                            }


                            Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

                            S Offline
                            S Offline
                            Scott Dorman
                            wrote on last edited by
                            #13

                            We are looking at two different versions of the Framework. What you show is correct for .NET 1.1. For .NET 2.0 and later, the implementation is what I described. Here are the full details: Since FileStream inherits the Close() and Dispose() methods from Stream, reflector shows the following for Stream:

                            public virtual void Close()
                            {
                            this.Dispose(true);
                            GC.SuppressFinalize(this);
                            }

                            public void Dispose()
                            {
                            this.Close();
                            }

                            protected virtual void Dispose(bool disposing)
                            {
                            if (disposing && (this._asyncActiveEvent != null))
                            {
                            this._CloseAsyncActiveEvent(Interlocked.Decrement(ref this._asyncActiveCount));
                            }
                            }

                            Since Flush() is an abstract method, the FileStream class implements it as:

                            public override void Flush()
                            {
                            if (this._handle.IsClosed)
                            {
                            __Error.FileNotOpen();
                            }
                            if (this._writePos > 0)
                            {
                            this.FlushWrite(false);
                            }
                            else if ((this._readPos < this._readLen) && this.CanSeek)
                            {
                            this.FlushRead();
                            }
                            }

                            ----------------------------- In just two days, tomorrow will be yesterday.

                            1 Reply Last reply
                            0
                            • C Colin Angus Mackay

                              Scott Dorman wrote:

                              I just double-checed that through Reflector

                              Ummm... I got my information through reflector. Dispose calls close on an object of a class called __HandleProtector, not FileStream.

                              public override void Close()
                              {
                              this.Dispose(true);
                              GC.nativeSuppressFinalize(this);
                              }

                              protected virtual void Dispose(bool disposing)
                              {
                              if (this._handleProtector != null)
                              {
                              if (!this._handleProtector.IsClosed)
                              {
                              this.Flush();
                              }
                              this._handleProtector.Close();
                              }
                              this._canRead = false;
                              this._canWrite = false;
                              this._canSeek = false;
                              this._buffer = null;
                              }

                              public override void Flush()
                              {
                              if (this._handleProtector.IsClosed)
                              {
                              __Error.FileNotOpen();
                              }
                              if (this._writePos > 0)
                              {
                              this.FlushWrite();
                              }
                              else if ((this._readPos < this._readLen) && this.CanSeek)
                              {
                              this.FlushRead();
                              }
                              }


                              Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

                              S Offline
                              S Offline
                              sharpiesharpie
                              wrote on last edited by
                              #14

                              err...so...what's the best way to do it? flush + close? and...how can i see the objects and classes? (the premade ones), the object browser right?

                              C 1 Reply Last reply
                              0
                              • S sharpiesharpie

                                err...so...what's the best way to do it? flush + close? and...how can i see the objects and classes? (the premade ones), the object browser right?

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

                                sharpiesharpie wrote:

                                err...so...what's the best way to do it? flush + close?

                                Since closing and disposing both get around to flushing the stream, why bother flushing it yourself?


                                Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

                                1 Reply Last reply
                                0
                                • S sharpiesharpie

                                  no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Hi, IMHO: Flush() is meaningful only if you dont plan on closing right away; it means "send this through, before I produce more". I use Flush() when tracing a program's progress to a file, so when the program suddenly crashes, the file contains all traces including the very last that was called. FileStream.Dispose(): on .NET 1.1 only exists as Dispose(bool) and is protected, you cant call it; you should call Dispose() for each object that offers a public Dispose(), but you cant call a protected one (unless you are inheriting from the class). on .NET 2.0 exists and is public but the doc says "is not intended to be used directly from your code", so dont call it ! Close() is the one that MUST be called, it tells everything is done. And according to the doc it calls Dispose(bool) itself. Conclusion: call either Flush() if you plan on producing more but want to make sure what you already have gets processed, or Close() if you're done. Dont call both. BTW looking at the (current) implementation is not a sound base for deciding what function to call, reading the doc is. :)

                                  Luc Pattyn [My Articles]

                                  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