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.
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刷新Stream和Unicode编码器也是如此.因此,一次调用StreamWriter.FlushAsync(),再加上await关键字以确保完全发生异步 *** 作,应该没问题.
总结以上是内存溢出为你收集整理的c# – 需要双重冲洗吗?全部内容,希望文章能够帮你解决c# – 需要双重冲洗吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)