Real and fake asynchronous I/O operations

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()); ...

Get Mastering C# Concurrency now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.