Most of them might have heard about the new “async” and “await” keyword which was introduced in C# 5.0. Today I will be discussing about that feature and how it will fundamentally change the way we code. It will have a big impact on all our future application which we will develop.
Why it’s required?
As we all know, Asynchronous and Parallel programming is very important style and it mainly helps to perfect the responsiveness the application. Right from the first release of .NET, Microsoft is supporting this style and with every new release of .NET Framework new ways of asynchronous features were introduced. In all the previous version, we as a Developer used to write Synchronous methods and used to call them asynchronously using “Thread, ThreadStart, ThreadPool, BackgroundWorker, etc.” but writing asynchronous methods itself was very tough to do. So now with “async” and “await”, Developer can create asynchronous method very easily. In fact, asynchronous code can now be almost identical to its synchronous counterpart.
Asynchronous methods looks something like this:
The method above will run synchronously until the await expression is reached, at which point the method is suspended and execution returns to the caller. The awaited Task is scheduled to run in the background on the same thread. As a convention, asynchronous method should be named in the form of xxxAsync.
Async methods can return Task, Task<T> or void. Task is simply an implementation of IAsynchResult. When method returns Task<T> then it is awaitable and that means you can call it using keyword await to suspend itself until after the task is finished.
How to do it?
Now let’s see how we can create Async methods.
· First let’s open Visual Studio and create a new Console Application called AsyncFileProcessing.
· Add the below code in the Main function:
· Next create the ProcessAsync method that will run inside the task created in the Main method. And add the below code in that function
· ProcessAsync will in turn call the other async function called as ReadFileAsync and it will have a below code
· Now build and run the application. As you see we are awaiting the reading of File to be completed. In the meantime if you see Console is still responsive and user can type on it.
· Once the task is completed, you can see the below result.
In the above example under Main method we simply create the ProcessAsync method which we start and wait for it to finish. Now within ProcessAsync method we have the following statement:
var data = await task;
The above statement tells the method to start the task and wait till it’s completed. And when its waiting application is not freezed and it is still responsive. And similarly if you look “ReadFileAsync” we have the below statement:
data = await reader.ReadToEndAsync();
Above statement reads the file data asynchronously.
Rules and Limitations
While usage of async and await is very flexible but still there are some limitation which we need to take care of. Below are the notable ones.
· In C# 5.0 you can’t use await in catch and finally block. Things have changed in C# 6.0 and Developers can actually use await in catch and finally block.
· Constructors and property accessors cannot be marked as async and use await
C# 5.0 and Framework 4.5 has taken a huge leap in terms of asynchronous programming and with new release of C# 6.0 more and more features are getting added in terms of Async programming. In the upcoming tutorial I will try to cover some additional async programming features.