In this article I will explain with an example, how to implement Asynchronous programming using Async and Await in ASP.Net using C# and VB.Net.
What is Asynchronous Programming?
Asynchronous programming is a method of parallel computer programming that enables a process to run separately from the primary function of the program.
Asynchronous programming helps to reduce or prevent wait times by enabling processes to continue to run in the background i.e. helps to improve application's performance and speed.
What is Async and Await?
An asynchronous operations is achieved using Async and Await keyword.
The method which we want to use asynchronous programming, we need to mark the method with the async keyword.
For asynchronous operations for which we do not want to block the execution, the await keyword needs to be use.
Await means the thread is free to execute other process and will come back when main thread operation is completed.
Note: When await keyword is used, async keyword must be used in the methods and events.
Async methods have the following return types:-
Task – Represents an asynchronous operation that do not return any value.
Task<TResult> – Represents an asynchronous operation that can return a value.
Note: For more details on Async return types, please refer the following MSDN article
Async return types.
HTML Markup
The following HTML Markup consists of:
Label – For displaying Name and Age.
<table>
<tr>
<td>Name:</td>
<td><asp:Label ID="lblName" runat="server" /></td>
</tr>
<tr>
<td>Age:</td>
<td><asp:Label ID="lblAge" runat="server" /></td>
</tr>
</table>
Namespaces
You need to import the following namespace.
C#
using System.Threading.Tasks;
VB.Net
Imports System.Threading.Tasks
Using Async and Await in C# and VB.Net
Inside the Page Load event handler, the SetName and SetAge methods are called which will be explained latter.
Inside this event handler, first the SetName method is called and Task.Delay() is executed and free the current thread. Then after 3 seconds, the thread will be called in order to run the SetAge method.
SetName
SetName method is defined with async keyword in order to make the method asynchronous.
Inside this method, the name is set in the Label control and task is delayed using Delay method and the keyword await is set.
Note: The Task.Delay() method does same as Thread.Sleep() does.
SetAge
SetAge method is defined with async keyword in order to make the method asynchronous.
Inside this method, the age is set in the Label control and task is delayed using Delay method and the keyword await is set.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.SetName();
this.SetAge();
}
}
private async Task SetName()
{
lblName.Text = "Mudassar Khan";
await Task.Delay(3000);
}
private async Task SetAge()
{
lblAge.Text = "38";
await Task.Delay(2000);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.SetName()
Me.SetAge()
End If
End Sub
Private Async Function SetName() As Task
lblName.Text = "Mudassar Khan"
Await Task.Delay(3000)
End Function
Private Async Function SetAge() As Task
lblAge.Text = "38"
Await Task.Delay(2000)
End Function
Screenshot
Downloads