当使用FileStream.ReadAsync()我应该开在异步模式下的文件? [英] When using FileStream.ReadAsync() should I open the file in async mode?

查看:499
本文介绍了当使用FileStream.ReadAsync()我应该开在异步模式下的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行异步I / O的旧的.Net方式的FileStream 是使用<一个href=\"http://msdn.microsoft.com/en-us/library/zxt5ahzw%28v=vs.110%29.aspx\"><$c$c>FileStream.BeginRead()和<一个href=\"http://msdn.microsoft.com/en-us/library/525xt96h%28v=vs.110%29.aspx\"><$c$c>FileStream.EndRead().

The old .Net way of performing asynchronous I/O for a FileStream is to use FileStream.BeginRead() and FileStream.EndRead().

MSDN文档FileStream.BeginRead()规定:

的FileStream提供了两种不同的操作模式:同步I / O和异步I / O。而任一,可以使用底层操作系统资源可能允许在只有这些模式中的一个的访问。

FileStream provides two different modes of operation: synchronous I/O and asynchronous I/O. While either can be used, the underlying operating system resources might allow access in only one of these modes.

在默认情况下,打开的FileStream操作系统处理同步。在Windows中,这会减慢异步方法。 如果使用异步方法,使用FileStream(字符串,的FileMode,FileAccess的,文件共享,的Int32,布尔)构造函数。

By default, FileStream opens the operating system handle synchronously. In Windows, this slows down asynchronous methods. If asynchronous methods are used, use the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.

执行异步I / O的的.Net 4.5倍方式的FileStream 是使用<一个href=\"http://msdn.microsoft.com/en-us/library/hh137813%28v=vs.110%29.aspx\"><$c$c>Stream.ReadAsync().

The .Net 4.5x way of performing asynchronous I/O for a FileStream is to use Stream.ReadAsync().

MSDN文档 FileStream.ReadAsync()直接到达的文档Stream.ReadAsync链接()。本文档没有提到任何需要打开在异步模式下的文件;事实上,文档中的样本code显然没有这样做。

The MSDN documentation for FileStream.ReadAsync() links directly to the documentation for Stream.ReadAsync(). This documentation does not mention any need to open the file in async mode; indeed, the sample code in the documentation manifestly does not do so.

因此​​,我认为,当使用 File.ReadAsync()没有必要在异步模式下打开文件。

I therefore assume that when using File.ReadAsync() there is no need to open the file in async mode.

这是假设是正确的?

我刚刚发现使用异步文件访问的MSDN文章。

I have just discovered an MSDN article on using Async for File Access.

本声明:

本主题中的示例使用FileStream类,其中有引起异步I / O在操作系统级别发生的选项。通过使用此选项,您可以避免在许多情况下阻塞线程池线程。

The examples in this topic use the FileStream class, which has an option that causes asynchronous I/O to occur at the operating system level. By using this option, you can avoid blocking a ThreadPool thread in many cases.

要启用该选项,可以指定useAsync = true或选项=在构造函数调用FileOptions.Asynchronous参数。

所以,现在我在想,我的的开设在异步模式下的文件......如果是这样,它有点不幸的是,文件中提供的样本code为 ReadAsync()做的的打开文件异步!

So now I'm thinking that I should be opening the file in asynchronous mode... If so, it's somewhat unfortunate that the sample code provided in the documentation for ReadAsync() does not open the file asynchronously!

推荐答案

在Win32中,你需要指定<一个href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa365683%28v=vs.85%29.aspx\">FILE_FLAG_OVERLAPPED要利用异步文件IO的。在.NET世界里,你用的FileStream isAsync 参数来实现相同的。如果你不这样做,操作不会是异步的。

In win32, you need to specify FILE_FLAG_OVERLAPPED to make use of asynchronous File IO. In .net world you use isAsync parameter of FileStream to achieve the same. If you fail to do so, operations will not be asynchronous.

这是一个耻辱, FileStream.ReadAsync 及其相关方法未能将其记录下来。

It is a shame that FileStream.ReadAsync and its related methods failed to document it.

您可以通过偷看进入实施证实了这一点。

You can confirm this by peeking into the implementation.

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
   ...
    if (!this._isAsync || !Environment.IsWindowsVistaOrAbove)
    {
        return base.ReadAsync(buffer, offset, count, cancellationToken);
    }
    ...
    return stateObject;
}

base.ReadAsync 最终将调用到 Stream.Read 线程池<方法同步/ code>给IM pression该操作是异步的,但实际上并不是。

base.ReadAsync will eventually call to Stream.Read method synchronously in ThreadPool giving the impression that operation is async, but really not.

并发编程相关的信息在Windows 书(PG:818):

Related information from Concurrent Programming On Windows book (Pg:818):

的CreateFile ,则必须在创建时指定您希望
  喜欢用一个FileStream异步执行。同
  的FileStream ,你通过传递正确的 isAsync 参数来做到这一点
  构造函数重载,它接受它。流的 IsAsync
  属性将随后返回true。如果你没有通过这个
  值,调用的BeginRead BeginWrite 会成功。但他们
  将利用流,它提供了基类的实现
  没有真正的异步文件I / O的好处。

As with CreateFile, you must specify at creation time that you'd like to use a FileStream for asynchronous execution. With FileStream, you do this by passing true as the isAsync argument to the constructor overloads, which accept it. The stream's IsAsync property will subsequently return true. If you fail to pass this value, calls to BeginRead and BeginWrite will succeed. But they will use the base class implementation from Stream, which provides none of the benefits of true asynchronous file I/O.

以上信息是关于APM的方法(因为这是老书),但仍然具有现实意义的。

Above information is about APM methods(as this is old book) but still relevant one.

这篇关于当使用FileStream.ReadAsync()我应该开在异步模式下的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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