Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

C# Programming Async and Await

4/21/2015

Comments

 
Introduction

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. 

Keywords

Asynchronous methods looks something like this:
Picture
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.

Task Class

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:
Picture
·         Next create the ProcessAsync method that will run inside the task created in the Main method. And add the below code in that function
Picture
·         ProcessAsync will in turn call the other async function called as ReadFileAsync and it will have a below code
Picture
·         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.
Picture
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

  • An async method cannot have a ref or out parameters
  • The await keyword may not be used in the body of a lock statement.


Summary

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.

Comments
comments powered by Disqus

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot