Reading filestream asynchronously in .NET 4.5

Microsoft .NET 4.5 makes all Stream-based communication support entirely asynchronous opertaions by adding Async methods to the Stream class.

int chunkSize = 4096;
var buffer = new byte[chunkSize];
var fileStream = new FileStream(filename, FileMode.Open);
var task = fileStream.ReadAsync(buffer, 0, buffer.Length);
task.ContinueWith((readTask) =>
    int amountRead = readTask.Result;
    Console.WriteLine("Async(Simple) read {0} bytes", amountRead);

Enjoyed this post? Share it!