FileReader.ReadAsync 真的是异步的吗?
Is FileReader.ReadAsync truly async?
所以最近我正在阅读 Richter 的书 CLR via C#。它是关于 .NET Framework 4.5 或其他东西的,他在那里说 FileReader.ReadAsync
不是真正的异步,它实际上只是对同步 win32
API 的异步包装。我不是 .NET Framework 开发人员,我使用 .NET Core。所以我有几个问题:
- 应用程序扩展有多糟糕,因为它用 Task.Run 之类的东西包装了同步功能?
- .NET Core 中是否修复了上述行为?如果我说
FileReader.ReadAsync
,它会异步读取吗?
- 如果您可以根据我调用的方法(例如
Read
或 ReadAsync
确定我希望操作是同步的还是异步的,那么 FileOptions.Asynchronous
有什么意义?
- 如果 WinRT 提供异步 reading/opening 而
win32
不提供,为什么不使用 WinRT?
FileStream.ReadAsync
始终从应用程序级别视图异步读取文件(此时调用未被阻塞,只是 return 控制用户代码)。
但这是真正的还是模拟的异步 I/O - 取决于 FileOptions.Asynchronous
option: from Using Async for File Access (C#)
this option 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
这是
的答案
What's the point of having that FileOptions.Asynchronous
来自书本
When you create a FileStream object, you get to specify whether you
want to communicate using synchronous or asynchronous operations via
the FileOptions.Asynchronous flag (which is equivalent to calling the
Win32 CreateFile function and passing into it the FILE_FLAG_OVERLAPPED
flag). If you do not specify this flag, Windows performs all
operations against the file synchronously. Of course, you can still
call FileStream’s ReadAsync method, and to your application, it looks
as if the operation is being performed asynchronously, but internally,
the FileStream class uses another thread to emulate asynchronous
behavior; use of this thread is wasteful and hurts performance. On the
other hand, you can create a FileStream object by specifying the
FileOptions.Asynchronous flag. Then you can call FileStream’s Read
method to perform a synchronous operation. Internally, the FileStream
class emulates this behavior by starting an asynchronous operation and
then immediately puts the calling thread to sleep until the operation
is complete. This is also inefficient, but it is not as inefficient as
calling ReadAsync by using a FileStream constructed without the
FileOptions.Asynchronous flag
所以这里声明 FileStream class 使用另一个线程来模拟异步行为,仅在您 未指定 FileOptions.Asynchronous
标志的情况下。
Why not use WinRT for async reading/opening if WinRT provides that and
win32 doesnt?
winRT 内部无论如何调用 win32(或本机)api。
所以最近我正在阅读 Richter 的书 CLR via C#。它是关于 .NET Framework 4.5 或其他东西的,他在那里说 FileReader.ReadAsync
不是真正的异步,它实际上只是对同步 win32
API 的异步包装。我不是 .NET Framework 开发人员,我使用 .NET Core。所以我有几个问题:
- 应用程序扩展有多糟糕,因为它用 Task.Run 之类的东西包装了同步功能?
- .NET Core 中是否修复了上述行为?如果我说
FileReader.ReadAsync
,它会异步读取吗? - 如果您可以根据我调用的方法(例如
Read
或ReadAsync
确定我希望操作是同步的还是异步的,那么FileOptions.Asynchronous
有什么意义? - 如果 WinRT 提供异步 reading/opening 而
win32
不提供,为什么不使用 WinRT?
FileStream.ReadAsync
始终从应用程序级别视图异步读取文件(此时调用未被阻塞,只是 return 控制用户代码)。
但这是真正的还是模拟的异步 I/O - 取决于 FileOptions.Asynchronous
option: from Using Async for File Access (C#)
this option 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
这是
的答案What's the point of having that FileOptions.Asynchronous
来自书本
When you create a FileStream object, you get to specify whether you want to communicate using synchronous or asynchronous operations via the FileOptions.Asynchronous flag (which is equivalent to calling the Win32 CreateFile function and passing into it the FILE_FLAG_OVERLAPPED flag). If you do not specify this flag, Windows performs all operations against the file synchronously. Of course, you can still call FileStream’s ReadAsync method, and to your application, it looks as if the operation is being performed asynchronously, but internally, the FileStream class uses another thread to emulate asynchronous behavior; use of this thread is wasteful and hurts performance. On the other hand, you can create a FileStream object by specifying the FileOptions.Asynchronous flag. Then you can call FileStream’s Read method to perform a synchronous operation. Internally, the FileStream class emulates this behavior by starting an asynchronous operation and then immediately puts the calling thread to sleep until the operation is complete. This is also inefficient, but it is not as inefficient as calling ReadAsync by using a FileStream constructed without the FileOptions.Asynchronous flag
所以这里声明 FileStream class 使用另一个线程来模拟异步行为,仅在您 未指定 FileOptions.Asynchronous
标志的情况下。
Why not use WinRT for async reading/opening if WinRT provides that and win32 doesnt?
winRT 内部无论如何调用 win32(或本机)api。