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,
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.
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.