异步/等待并打开一个FileStream? [英] async/await and opening a FileStream?

查看:164
本文介绍了异步/等待并打开一个FileStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到下面的问题就来了时,试图确定,如果我用的是的方法,如 ReadAsync CopyToAsync 正确:
<一href=\"http://stackoverflow.com/questions/18331349/c-sharp-4-5-file-read-performance-sync-vs-async\">C# 4.5文件的读取性能同步异步VS

在这个问题我在阅读接受的答案如下:


  

最值得注意的是,你的异步测试并没有使用异步I / O;与文件
  溪流,你必须明确地打开它们作为异步要不然
  你只是在做一个后台线程同步操作。


在他的异步IO code他用下面打开的FileStream 异步:

  var文件=新的FileStream(文件名,FileMode.Open,FileAccess.Read,FileShare.Read,4096,真)

所以,如果你打算使用的方法,如 CopyToAsync 您是否应该打开的基本的FileStream ,因为我想知道上面显示?,而不是做一些简单的像下面这样:

  File.Open(文件名,FileMode.Open)

这是怎么了 CopyToAsync 实际的文档中的示例演示打开底层的FileStream
https://msdn.microsoft.com/en-美国/库/ hh159084(v = vs.110)的.aspx

如果不要紧哪种方式的根本的FileStream 打开,什么是的 useAsync 参数的FileStream 构造办?


解决方案

  

所以我在想,如果你打算使用的方法,如CopyToAsync你是否应该打开底层的FileStream如上图所示?


是的。究其原因主要是历史性的。

首先,在Windows上,<一个href=\"https://msdn.microsoft.com/en-us/library/windows/desktop/ms686358(v=vs.85).aspx\"><$c$c>HANDLEs (包括文件句柄)必须打开/如果你想要做异步他们( OVERLAPPED )操作与异步标志明确创建的。

不过,旧的Windows 95/98 / ME行的只有的串行端口和IOCTL在支持异步操作(设备驱动程序)处理。异步I上/ O的磁盘文件的是不支持该平台行。和原.NET的没有的支持98 / ME ,所以原来的的FileStream 只是用来同步I / O。我的认为的(但我没有绝对的把握),该 APM方法(如 FileStream.BeginRead )在Win98 / ME很可能只是用所谓的异步委托(这只是执行像同步方法 FileStream.Read 上一个线程池线程)。

所以,这是历史的原因,文件流句柄是的的缺省使用异步标志打​​开。


  

这是何等的CopyToAsync实际文档中的示例演示


不幸的是,很多MSDN的例子是质量,而差。他们是OK,如果你从这里有一个如何调用该特定方法的示例的角度看待他们,但是从的角度来看没有那么大在这里是一个使用此方法的生产质量code榜样

I came across the following question when trying to determine if I was using the Stream methods such as ReadAsync and CopyToAsync correctly: C# 4.5 file read performance sync vs async

In this question I read the following in the accepted answer:

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.

In his asynchronous IO code he was using the following to open the FileStream 'asynchronously':

var file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)

So I was wondering if you intend to use methods such as CopyToAsync whether you should open the underlying FileStream as shown above?, as opposed to doing something simple like the following:

File.Open(filename, FileMode.Open)

Which is how the example in the actual documentation for CopyToAsync demonstrates opening the underlying FileStream: https://msdn.microsoft.com/en-us/library/hh159084(v=vs.110).aspx

If it does not matter which way the underlying FileStream is opened, what does the useAsync parameter of the FileStream constructor do?

解决方案

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

Yes. The reason is mostly historical.

First, on Windows, HANDLEs (including file handles) must be opened/created explicitly with an asynchronous flag if you want to do asynchronous (OVERLAPPED) operations on them.

However, the old Windows 95/98/ME line only supported asynchronous operations on serial port and IOCTL (device driver) handles. Asynchronous I/O on disk files wasn't supported on that platform line. And the original .NET did support 98/ME, so the original FileStream just used synchronous I/O. I think (but am not absolutely sure) that APM methods (like FileStream.BeginRead) on Win98/ME were probably just implemented using the so-called "asynchronous delegates" (which just execute a synchronous method like FileStream.Read on a thread pool thread).

So, that's the historical reason why file stream handles were not opened with the asynchronous flag by default.

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

Unfortunately, a lot of the MSDN examples are rather poor quality. They're OK if you approach them from the perspective of "here's an example of how to call this specific method", but not so great from the perspective of "here's an example of production-quality code that uses this method".

这篇关于异步/等待并打开一个FileStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆