gpt4 book ai didi

c# - 需要双冲吗?

转载 作者:太空狗 更新时间:2023-10-29 21:23:23 26 4
gpt4 key购买 nike

是否需要在刷新 StreamWriter 之后刷新 Stream

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?
}

最佳答案

根据 MSDN 文档,这可以被原谅为“只是确保”...

StreamWriter.Flush():

Clears all buffers for the current writer and causes any buffered data to be written to the underlying 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.

...但是,仔细查看 TypeDescriptor 中的代码会发现 StreamWriter.Flush()(我假设它的异步对应物 FlushAsync)是调用主函数的重载,传递两个指示 StreamWriter 的 bool 参数刷新 Stream 和 Unicode 编码器。因此,调用 StreamWriter.FlushAsync() 并结合 await 关键字以确保异步操作已完全发生,应该没问题。

关于c# - 需要双冲吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13869251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com