Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double flush required?

Is it necessary to flush a Stream after flushing a StreamWriter?

public static async Task WriteStringAsync(this Stream stream, string messageString)
{
        var encoding = new UTF8Encoding(false); //no BOM
        using (var streamWriter = new StreamWriter(stream, encoding))
        {
            await streamWriter.WriteAsync(messageString);
            await streamWriter.FlushAsync();
        }
        await stream.FlushAsync(); //is this necessary?
}
like image 795
spender Avatar asked Dec 13 '12 22:12

spender


People also ask

Why does my toilet require two flushes?

If your toilet is flushing twice, it is most likely due to the fact that it is staying open too long and flushing too much water. If you have an adjustable flapper, this can be corrected by adjusting your toilet flapper to close quicker.

Are double flush toilets worth it?

A dual flush toilet drives lower water usage in your home, thereby saving money on your monthly water bill. The Environmental Protection Agency (EPA) estimates that 4,000 gallons of water can be saved annually in homes that use dual flush toilets.

What is meant by dual flush?

Since their inception in 1980, dual-flush toilets were made to reduce the amount of water flushed during use. They feature two different buttons to perform two different kinds of flushes. The difference in buttons depends on the waste in the toilet. One button for liquid waste, another for solid waste.


1 Answers

According to the MSDN docs, this could be forgiven as "just making sure"...

StreamWriter.Flush():

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

Stream.Flush():

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

... However, a closer look at the code in TypeDescriptor shows that StreamWriter.Flush() (and I would assume its asynchronous counterpart FlushAsync) is an overload that calls the primary function, passing two Boolean parameters which instruct the StreamWriter to flush the Stream and the Unicode encoder as well. So, one call to StreamWriter.FlushAsync(), coupled with the await keyword to ensure the async operation has happened completely, should be fine.

like image 78
KeithS Avatar answered Oct 19 '22 18:10

KeithS