locked
About streamWrite.Flush()'s location RRS feed

  • Question

  • I want to write the query result to a file. Here are two codes. The difference is the Flush() method in the different location.

    The first one:

    StreamWriter sr = new StreamWriter(fileName,false);
    sr.WriteLine("Start to retrieve " );
    while (!something)
    {
    	var query = drContext.DRTable.Where(c.OffHook < aValue)
    		.OrderBy(c => c.OffHook)
    		.ToList();
    	foreach (var y in query)
    	{
    		sr.WriteLine(y.FileName);
    	}
    	sr.Flush();
    }

    The second one:

    StreamWriter sr = new StreamWriter(fileName,false);
    sr.WriteLine("Start to retrieve " );
    while (!something)
    {
    	var query = drContext.DRTable.Where(c.OffHook < aValue)
    		.OrderBy(c => c.OffHook)
    		.ToList();
    	foreach (var y in query)
    	{
    		sr.WriteLine(y.FileName);
    		sr.Flush();
    	}
    }

    Which one is correct?

    • Changed type Mike Feng Monday, February 11, 2013 9:16 AM
    Friday, February 8, 2013 1:51 PM

Answers

  • You should be disposing of your stream when you're done with it so that it can release it's unmanaged resources.  The most effective way of doing this is to put it in a `using` block.

    When the stream is disposed at the end of the using it will be flushed.

    There is no compelling reason for you to flush the buffer after writing every line.  It will slow it down a bit and add no real perceptible benefits.  As a rule, as long as you are closing your streams when you're done with them you generally don't need to use `Flush`.  There are occasional times where it's useful, but it shouldn't be often.

    • Marked as answer by ardmore Monday, February 11, 2013 1:11 PM
    Friday, February 8, 2013 3:05 PM

All replies

  • Hi,

    In your case I'm not even sure that you need to use StreamWriter.Fluch() as your are writing through the pipe to your file. .Flush() as far as I'm aware of will only clean the buffer of your StreamWriter.

    You should make some test with a consoleApplication to run these different cases.

    Friday, February 8, 2013 2:01 PM
  • You should be disposing of your stream when you're done with it so that it can release it's unmanaged resources.  The most effective way of doing this is to put it in a `using` block.

    When the stream is disposed at the end of the using it will be flushed.

    There is no compelling reason for you to flush the buffer after writing every line.  It will slow it down a bit and add no real perceptible benefits.  As a rule, as long as you are closing your streams when you're done with them you generally don't need to use `Flush`.  There are occasional times where it's useful, but it shouldn't be often.

    • Marked as answer by ardmore Monday, February 11, 2013 1:11 PM
    Friday, February 8, 2013 3:05 PM
  • This is as far as I know myself. By default many streams (inlcuding Console output streams and file streams) "buffer" output.
    The reason is that the Filesystem might need some time to actually get to the writing process but the applications should continue to run smoothly after the bytes are put into it

    Closing() and/Disposing() also call Flush() before doing thier work.
    Also some functions might impilcitly flush (WriteLine leaps to mind).
    sr.Write(y.FileName + Environment.newLine) would not nessesarily flush.

    About the position:

    Honestly you don't need it. It's a dozen times more important to properly close/dispose the Stream after use, then to flush it. I never had problems using "using" to Dispose of the stream:

    using (StreamWriter sr = new StreamWriter(filename)){
      //write stuff into the file
    } //sr.Dispose is called; Dispose calls close and close calls flush

    Friday, February 8, 2013 3:10 PM
  • I agree with Servy, there is no need of using a flush each time the Writer is called.

    However the Using statement should be use and it particularly well fit this kind of action. Always thing about put your disposable object into a 'using' statement to nicely dispose and close this kind of object.

    Friday, February 8, 2013 3:32 PM