Прочетете на английски Редактиране

Споделяне чрез


BufferedStream.Flush Method

Definition

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

C#
public override void Flush();

Exceptions

The stream has been disposed.

The data source or repository is not open.

Examples

This code example is part of a larger example provided for the BufferedStream class.

C#
// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
{
    bufStream.Write(dataToSend, 0, dataToSend.Length);
}
bufStream.Flush();
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
    numberOfLoops * dataToSend.Length,
    bufferedTime.ToString("F1"));

Remarks

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close.

If you use the BufferedStream constructor, thus specifying the buffer size while creating the BufferedStream object, the content is flushed when it reaches the buffer size. For example, code such as BufferedStream bs = new BufferedStream(bs, 5) will flush the content when the buffer size reaches 5 bytes.

All the read and write methods of BufferedStream automatically maintain the buffer, so there is no need to invoke Flush when switching back and forth between reading and writing.

Applies to

Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.5, 1.6, 2.0, 2.1

See also