Supporting Asynchronous IO Operations with .NET Framework 4.5

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!



Related Articles

Comments

  • Just a dev

    Posted by Freddy on 01/30/2014 12:45am

    Do you have a source for your statement that in the early days of .net "writing code to perform asynchronous IO operations was not possible and hence the IO operations had to be synchronous" I'm particularly interested in the statement that .net 4 introduced async IO. Of course we've had async operations for a long time now, so I wonder if you are referring to actual async IO (IOCP and overlapped IO). If that is the case, it'd be great if you can share the source on that, as I am having trouble to find a good source on .net for support for this. ps. I guess another option that may have happened, is that you are talking about the changes in the async patterns over the years, which certainly has made it a lot better.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date