In the earlier versions of .NET framework, writing code to perform asynchronous IO operations was not possible and hence the IO operations had to be synchronous. The problems that the developers were encountering with the synchronous approach were:

1. Unresponsiveness of UI - if the application is a thick client and had to perform file IO operations based on the user actions.

2. Performance issue - In case of back ground process, where it has to process large files.

In .NET Framework 4.0 asynchronous IO provisions were given for classes like StreamReader, StreamWriter, etc. through the methods BeginRead, BeginWrite, etc., involving callbacks. Though it provided a way to write asynchronous code there was yet another drawback--the code complexity!


In .NET Framework 4.5 the IO classes are packed with new Async methods using await and async keywords, which can be used to write straight-forward and clean asynchronous IO code. Below are the advantages of using these new async IO methods.


1. Responsive UI - In Windows apps, the user will be able to perform other operations while the IO operation is in progress.

2. Optimized performance due to concurrent work.
3. Less complexity - as simple as synchronous code.

In this article we look at a few examples of async IO operations in .NET Framework 4.5.


StreamReader and StreamWriter

StreamReader and StreamWriter are the widely used file IO classes in order to process flat files (text, csv, etc). The 4.5 version of .NET Framework provides many async methods in these classes. Below are some of them.


1.ReadToEndAsync
2.ReadAsync
3.ReadLineAsync
4.FlushAsync – Reader
5.WriteAsync
6.WriteLineAsync
7.FlushAsync – Writer

The code below reads the content from a given list of files asynchronously.

namespace AsyncIOSamples
{
class Program
{
static void Main(string[] args)
{
List<string> fileList = new List<string>()
{
"DataFlatFile1.txt",
"DataFlatFile2.txt"
};
foreach (var file in fileList)
{
ReadFileAsync(file);
}
Console.ReadLine();
}
private static async void ReadFileAsync(string file)
{
using (StreamReader reader = new StreamReader(file))
{
//Does not block the main thread
string content = await reader.ReadToEndAsync();
//Gets called after the async call is done.
Console.WriteLine(content);
}
}
}
}


Now let us try with the ReadLineAsync and read the content from a single file asynchronously.

namespace AsyncIOSamples
{
class Program
{
static void Main(string[] args)
{
ReadFileLineByLineAsync("DataFlatFile1.txt");
Console.WriteLine("Continue with some other process!");
Console.ReadLine();
}
private static async void ReadFileLineByLineAsync(string file)
{
using (StreamReader reader = new StreamReader(file))
{
string line;
while (!String.IsNullOrEmpty(line = await reader.ReadLineAsync()))
{
Console.WriteLine(line);
}
}
}
}
}


In these examples the main point to note is that these asynchronous operations do not block the main thread and are able to utilize the concurrency factor.

A similar example holds good for StreamWriter as well. Here is the sample code, which reads the content from a list of files and writes it to the output files without blocking the main thread execution.

namespace AsyncIOSamples
{
class Program
{
static void Main(string[] args)
{
ProcessFilesAsync();
//Main thread is not blocked during the read/write operations in the above method
Console.WriteLine("Do something else in the main thread mean while!!!");
Console.ReadLine();
}
private static async Task ProcessFilesAsync()
{
List<string> fileList = new List<string>()
{
"DataFlatFile1.txt",
"DataFlatFile2.txt"
};
foreach (var fileName in fileList)
{
string content = await ReadFileAsync(fileName);
WriteFileAsync(content, "Output" + fileName);
}
}
private static async void WriteFileAsync(string content, string outputFileName)
{
using (StreamWriter writer = new StreamWriter(outputFileName))
{
await writer.WriteAsync(content);
}
}
private static async Task<string> ReadFileAsync(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
{
return await reader.ReadToEndAsync();
}
}
}
}


WebClient

This class is used for data request operations over protocols like HTTP, FTP, etc. This class is also bundled with a bunch of Async methods like DownloadStringTaskAsync, DownloadDataTaskAsync and more.

It doesn't end here but extends to classes like XmlReader, TextReader and many more. I will leave it to the readers to explore them.

Happy reading!