4

I'm creating a very big file using a FileStream (0.1 - 100 GBytes):

using (var strm = File.OpenWrite(Destination)) {
    while(someCondition) {
        bfr = GetBuffer();

        strm.Write(bfr.Data, 0, ChunkSizeInBytes);
        strm.Flush();

        ShowProgress();
    }
}

When I get to the end of the using statement, the thread hangs for a long time. I put a strm.Close() after the loop, and it seems that this is the jamming point (the file closure).

(please note that I Flush() after each Write())

Why is it and how to overcome it ?

svick
  • 236,525
  • 50
  • 385
  • 514
Tar
  • 8,529
  • 9
  • 56
  • 127
  • 3
    I may be wrong, but `Flush(true)` is what flushes it out to disk. `Flush()` will only flush it to an internal buffer. – Simon Whitehead Jun 25 '13 at 10:42
  • http://msdn.microsoft.com/en-us/library/system.io.filestream.flush.aspx what may be happening is that Flish() alone doesn't clear intermediate buffer's. So they pile up information that probably is being cleared by the GC at Close(), this last part is a supposition on my part :) – CarlosB Jun 25 '13 at 10:46

1 Answers1

4

My comment was correct.

See Hans Passant's answer here: https://stackoverflow.com/a/4921728/1517578

Flush(true) will flush out to disk immediately. Flush() will not.

Community
  • 1
  • 1
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138