So far, an asynchronous I/O seems to be a good thing for server applications. Unfortunately, there is quite unexpected underwater stone that is very hard to find. Let's look at the following code. It happens that the FileStream
instance has the IsAsync
property, indicating that the underlying I/O operation is asynchronous. We will start a few asynchronous writes and check whether they are really asynchronous:
private const int BUFFER_SIZE = 4096; private static async Task ProcessAsynchronousIO() { using (var stream = new FileStream("test1.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE)) { Console.WriteLine("1. Uses I/O Threads: {0}", stream.IsAsync); var buffer = Encoding.UTF8.GetBytes(CreateFileContent()); var t = stream.WriteAsync(buffer, 0, buffer.Length); await t; } using (var stream = new FileStream("test2.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, BUFFER_SIZE, FileOptions...