c# – 异步等待并打开FileStream?

c# – 异步等待并打开FileStream?,第1张

概述尝试确定是否正在正确使用ReadAsync和CopyToAsync等Stream方法时遇到以下问题: C# 4.5 file read performance sync vs async 在这个问题中,我在接受的答复中看到以下内容: Most notably, your “async” test does not use async I/O; with file streams, you have 尝试确定是否正在正确使用ReadAsync和copyToAsync等Stream方法时遇到以下问题:
C# 4.5 file read performance sync vs async

在这个问题中,我在接受的答复中看到以下内容:

Most notably,your “async” test does not use async I/O; with file
streams,you have to explicitly open them as asynchronous or else
you’re just doing synchronous operations on a background thread.

在他的异步IO代码中,他正在使用以下内容来异步打开fileStream:

var file = new fileStream(filename,fileMode.Open,fileAccess.Read,fileShare.Read,4096,true)

所以我想知道如果你打算使用诸如copyToAsync之类的方法,无论你是否打开如上所示的底层fileStream,而不是像下面这样做一些简单的事情:

file.Open(filename,fileMode.Open)

copyToAsync的实际文档中的示例如何演示打开底层fileStream:
https://msdn.microsoft.com/en-us/library/hh159084(v=vs.110).aspx

如果打开底层fileStream的方式无关紧要,fileStream构造函数的useAsync参数是做什么的?

解决方法

So I was wondering if you intend to use methods such as copyToAsync whether you should open the underlying fileStream as shown above?

是.原因大多是历史的.

首先,在windows上,HANDLEs (including file handles) must be opened/created explicitly with an asynchronous flag if you want to do asynchronous (OVERLAPPED) operations on them.

但是,the old Windows 95/98/ME line only supported asynchronous operations on serial port and IOCTL (device driver) handles.该平台线路不支持磁盘文件上的异步I / O.而the original .NET did support 98/ME,所以原来的fileStream只是使用同步I / O.我认为(但不是绝对肯定)Win98 / ME上的APM methods(如fileStream.BeginRead)可能只是使用所谓的“asynchronous delegates”(在线程池线程上执行fileStream.Read的同步方法)来实现.

所以,这是为什么文件流处理默认情况下不使用异步标志打开的历史原因.

Which is how the example in the actual documentation for copyToAsync demonstrates

不幸的是,很多MSDN的例子都是质量差的.如果您从“以下是一个如何调用此特定方法的示例”的角度来接近他们,则可以使用它们,但从“以下是使用此方法的生产质量代码的示例”的角度来看,并不那么好.

总结

以上是内存溢出为你收集整理的c# – 异步/等待并打开FileStream?全部内容,希望文章能够帮你解决c# – 异步/等待并打开FileStream?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1244067.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存