这是一篇知识库文章,其中列出了所有可能导致您希望异步执行的代码实际同步运行的方式。
异步磁盘 I/O 在 Windows NT、Windows 2000 和 Windows XP 上显示为同步
清单上的内容是否适用于您的情况?
您是否尝试过使用 .NET 4.5 的ReadAsync 方法实现相同的行为?
我从 MSDN 引用:
在 .NET Framework 4 及更早的版本中,您必须使用 BeginRead 和 EndRead 等方法来实现异步 I/O 操作。这些方法在 .NET Framework 4.5 中仍然可用以支持旧代码;但是,ReadAsync、WriteAsync、CopyToAsync 和 FlushAsync 等新的异步方法可以帮助您更轻松地实现异步 I/O 操作。
编辑 我正在使用 ICH10 和 Windows 7 在 OCZ Vertex 2 上使用 256MB 文件重现您的问题。我必须生成文件,重新启动 PC 以清除文件缓存,然后尝试读取相同的文件。
using System;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string fileName = @"C:\Temp\a1.txt";
int arraySize = 512 * 1024 * 1024;
var bytes = new byte[arraySize];
new Random().NextBytes(bytes);
// This prints false, as expected for async call
var callback = new AsyncCallback(result =>
Console.WriteLine("Completed Synchronously: " + result.CompletedSynchronously));
try
{
// Use this method to generate file...
//WriteFileWithRandomBytes(fileName, arraySize, bytes, callback);
Console.WriteLine("ReadFileAsync invoked at " + DateTimeOffset.Now);
var task = ReadFileAsync(fileName);
Console.WriteLine("ReadFileAsync completed at " + DateTimeOffset.Now);
Task.WaitAll(task);
Console.WriteLine("Wait on a read task completed at " + DateTimeOffset.Now);
}
finally
{
if (File.Exists(fileName))
File.Delete(fileName);
}
}
private static void WriteFileWithRandomBytes(string fileName, int arraySize, byte[] bytes, AsyncCallback callback)
{
using (var fileStream = new FileStream(fileName,
FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 128 * 1024, FileOptions.Asynchronous))
{
Console.WriteLine("BeginWrite invoked at " + DateTimeOffset.Now);
var asyncResult = fileStream.BeginWrite(bytes, 0, arraySize, callback, null);
Console.WriteLine("BeginWrite completed at " + DateTimeOffset.Now);
// completes in 6 seconds or so... Expecting instantaneous return instead of blocking
// I expect runtime to block here...
Task.WaitAll(Task.Factory.FromAsync(asyncResult, fileStream.EndWrite));
// or at least when flushing the stream on the following end-curly
}
}
private static Task<int> ReadFileAsync(string filePath)
{
FileInfo fi = new FileInfo(filePath);
byte[] buffer = new byte[fi.Length];
var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 64 * 1024, FileOptions.Asynchronous);
Task<int> task = Task<int>.Factory.FromAsync(file.BeginRead, file.EndRead, buffer, 0, buffer.Length, null);
return task.ContinueWith(t =>
{
file.Close();
Console.WriteLine("Done ReadFileAsync, read " + t.Result + " bytes.");
return t.Result;
});
}
}
}
当所有其他方法都失败时,这里是对非托管 API 文档的参考。