Asynchronous Programming in C#

 Introduction:

In this article we will learn what is Asynchronous programming in C#, In the old days, when we used to perform any time consuming task, the application used to get stuck or freeze and so it happened to be something very bad in terms of the performance of the application.

Asynchronous programming in C# introduces two keywords Async and Await, with the help of which we can perform heavy tasks in c# asynchronously, by asynchronous, we mean that a particular time consuming task does not have to wait for another task to get completed, and so it would not freeze the entire application, while it tries to perform a heavier or a time consuming task.

As we mentioned earlier that before asynchronous programming, In synchronous mode, a task needed to wait for another task to complete its execution and so the other task had to wait for the 1st one to complete its execution.

We will see how does asynchronous programming work in C# with an example, So first up create a new console application in visual studio 2017.

Step 1:

 

Step 2:

So, here we will consider an example with Asynchronous programing by creating two methods, we will name them as method1 and method2.


So, in the above code, we can see that we have 2 methods being defined, method1 and method2 and both of those methods are independent of each other, means that the 1st method is not waiting for the 2nd or the 2nd one is not waiting for the 1st method to complete its execution.

So if a method is defined with an async keyword, then we must have to use the await within that method as this is how it works. Async also indicates to the fact that it is actually an asynchronous method, 

Task.Delay() actually delays a task in milliseconds but it in asynchronous programming, the control shifts to the next line as it does not wait for any task to complete its execution.

Step 3:

We will call the two methods, the first one is an asynchronous method, and it shifts the control to the next line without completing its execution.

Here is how the entire code looks like,



At line 13 the first method method1 gets called and at line 14 the second method method2 gets called, 
line 19 indicates that we have used the await keyword before Task.Run, so await actually works in an asynchronous mode, and it shifts the control to the rest of the application, if it tries to run a long running process or a time consuming task.
line 24 and line 35 delays the task by milliseconds but it does not freeze up the application, as it shifts the control to the rest of the program.
At last we will run the application by pressing crtl+F5 to see the output window.

 So as we can see in the output window, that a time consuming method does not wait for another method to complete its execution, which clearly shows that the whole process is asynchronous and both methods are completely independent of each other. 

Example 2:

In this second we will consider a real time example of demonstrating the concept of asynchronous programing in c#,

Here we will read some data from a txt file, which is indeed a long running process and a time consuming job, so we will see how we can actually read data from a file without freezing or blocking the rest of the application.


In the code above, we have created a method called ReadFile() which is an asynchronous method and it takes the file path of a txt file as a parameter and returns the total number of words within that txt file, reader.ReadToEndAsync() reads the entire file till it ends.

The methods its self returns the length of the txt file.



The code  above demonstrates that an asynchronous method which is ReadFile() will be called, so the control will be shifted to that method, but since it is asynchronous so it does not block the rest of the application as it takes the control away from the method and executes the rest of the application, Now since we know that file reading process can be time consuming, so the program being in an asynchronous mode does not block the process and executes the rest of the code.
Here is how the Main function looks like.
 

So here we can see at line 14 that the async method call method() will be called, and the control will be shifted to that method, from there it runs the long running process without blocking the rest of the application.

Press ctrl+F5 to run the application and see the output window.


 So Here we can see that the asynchronous method gets called before but since it was a long running process so that the compiler executes the rest of the code and when the file reading process gets completed, so it outputs the corresponding message along with the total number of words count within that txt file.

Talha Mahmood

I have been developing Applications in Microsoft .NET technologies since 2015, and having more then 5 years of experience in .NET Technology and SQL database, also having hands in tools like C# Windows Forms, WPF, WCF, Asp.Net Web forms, Asp.Net Core, SSRS, LINQ, Entity Framework, Crystal Reports, SQL server, MySQL, MongoDB, Document DB, JQuery, JavaScript, Oracle, Web API.

Post a Comment

Previous Post Next Post