Difference between flush\dispose\close
-
no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)
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
-
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?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()
callsDispose()
which, in turn, callsFlush()
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
-
no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)
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;
-
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()
callsDispose()
which, in turn, callsFlush()
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
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 callClose
, which then calls a protectedDispose(bool)
method which actually does all of the work.----------------------------- In just two days, tomorrow will be yesterday.
-
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?Dispose
andClose
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.
-
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 callClose
, which then calls a protectedDispose(bool)
method which actually does all of the work.----------------------------- In just two days, tomorrow will be yesterday.
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
, notFileStream
.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
-
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
, notFileStream
.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
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?
-
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
, notFileStream
.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
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()
andDispose()
methods fromStream
, reflector shows the following forStream
: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, theFileStream
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.
-
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?
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
-
no, you can't dispose after you close...you have to dispose before (if you dispose after you close it throws an exception)
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]