async and await in .Net Framework 4.5

Let me start with the difference between synchronous process and asynchronous process.

As the above image depicts, we usually consider asynchronous process in case of a time taking task. Making an asynchronous call, we will execute the tedious task on a separate thread and will continue with remaining process on initial thread. Thus we will be able to execute both the tasks paralelly.

Until now we have been using a standard process of calling a function or method asynchronously.
Until .Net framework 4.0 below is the typical process of executing a method asynchronously.

I created a small sample application  in Visual Studio 2013.
Before we go into more details, i would like to highlight a simple fact.

"Until .Net Framework 4.0 we call a non-asynch method asynchronously, but from .Net Framework 4.5 we have asynch methods which reduces lot of efforts in development perspective."

Below is the class which has a time taking task.

"Don't worry about screenshots of code, link is available to download at the bottom"

Now look at the code in Program.cs making asynchronous call to above shown "NonAsynchMethod()".

"Don't worry about screenshots of code, link is available to download at the bottom"

Let me explain step by step.

1. First we need to identify which process we need to call asynchronously. Typically we consider a time taking tasks like making a call to a web service or  reading a file or accessing some external resources. For time being i just used a method which causes 3 seconds delay in execution.
Refer to "NonAsynchMethod()" in AsynchDemo class.

2. Instantiate and point the delegate to the "NonAsynchMethod".

3. Now we need to understand that delegateObject.BeginInvoke() will begin execution of tedious task on different thread and delegateObject.EndInvoke() will get the result. Now its up to you how you want your mail program to be. You can place both the statements in one procedure and perform  other tasks between these two tasks.
But if you want to use "Fire and Forget" implementation, you need to use a callback method.
I have chosen a callback method.

4. Create a call back method for performing what you have to do after getting the result from the tedious task. We need to call delegateObject.EndInvoke() method here to read the result.

5. In main program you need to create a delegate for this Callback method and use it with conjunction with BeginInvoke() method. So that it knows which method to call when the time taking task finishes off.

I created a time stamp at the end of every line of output so that you understand the flow.

Now we will look at async keyword in .Net Framework 4.5

I added a new method to AsyncDemo class.

Both async and await are the two keywords we need to concentrate here.
When you specify async keyword to a method, framework will treat that method as Asynchronous method which executes on a different thread and treat the output of this method as awaitable.

I added couple of more lines to my main program on how to call this async method.

Here the my async method returns nothing, so it must have return type as Task.
If a method returns a string the return type of async method should be Task<string>.
for more detail about syntax please refer to MSDN

Now i execute the program and here is the output.

Both executed method asynchronously, but look easy to implement with Framework 4.5 async and await keywords.

The complete project can be downloaded at this link and search for async and await.Zip 

1 comment:

  1. Superb explanation & it's too clear to understand the concept as well, keep sharing admin with some updated information with right examples.Keep update more posts.

    Car Wash Services in Mumbai