Introduction
C# supports both synchronous and asynchronous methods. Let's learn the difference between synchronous and asynchronous and how to code in C#.
Interestingly enough, any method we normally create in C# is synchronous by default. For example, the following method fetches data from a database and binds it to a TextBox synchronously.
private void LoadData() {
// Create connection
SqlConnection conn = new SqlConnection(@ "network address= .; integrated
security = true; database = EmployeeDb ");
// Create command
string sql = @ "select EmpId,Name
from dbo.EmployeeDetails where EmpID <= 500 ";
// Data binding code goes here
try {
// Open connection
conn.Open();
// Execute query via ExecuteReader
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) {
txtReader.AppendText("\nEmpID: ");
txtReader.AppendText(rdr.GetValue(1) + "\t\t" + rdr.GetValue(0));
txtReader.AppendText("\n");
}
} catch (SqlException ex) {
MessageBox.Show(ex.Message + ex.StackTrace, "Exception Details");
} finally {
conn.Close();
}
}
What is Synchronous in C#
-
Synchronous represents a set of activities that starts happening together at the same time.
-
A synchronous call waits for the method to complete before continuing with program flow.
How bad is it?
-
It badly impacts the UI that has just one thread to run its entire user interface code.
-
Synchronous behavior leaves end users with a bad user experience and a blocked UI whenever the user attempts to perform some lengthy (time-consuming) operation.
Business Scenario and Problem Statement
Consider a real-world business case in which a UI binds data to the data grid by fetching it from the database. While data is being fetched and bound to the grid the rest of the UI is blocked. Any attempt of interaction with other UI controls will not be evident until the data loading is over. This UI blockage gets over when data fetch-and-binding is completely done. Refer to "Figure 1-1 Synchronous Behavior" below

Figure 1-1 Synchronous Behavior
Solution to the Synchronous Problem
A synchronous method call can create a delay in program execution that causes a bad user experience. Hence, an asynchronous approach (threads) will be better. An asynchronous method call (cretion of a thread) will return immediately so that the program can perform other operations while the called method completes its work in certain situations.
The asynchronous method's behavior is different than synchronous ones because an asynchronous method is a separate thread. You create the thread; the thread starts executing, but control is immediately returned back to the thread that called them time; while the other thread continues to execute.
In general, asynchronous programming makes sense in two cases as,
-
If you are creating a UI intensive application in which the user experience is the prime concern. In this case, an asynchronous call allows the user interface to remain responsive. Unlike as shown in Figure 1-1.
-
If you have other complex or expensive computational work to do, you can continue; interacting with the application UI while wait for the response back from the long-running task.
Asynchronous Patterns in C#
There are various ways to use threads in applications. These recipes are known as Patterns.
Asynchronous Programming Model Pattern
-
Relies on two corresponding methods to represent an asynchronous operation: BeginMethodName and EndMethodName
-
Most often you must have seen this while using delegates or method invocation from a Web Service.

Figure 1-2 APM Pattern
Event Based Asynchronous Pattern
-
The Event-based Asynchronous Pattern has a single MethodNameAsync method and a corresponding MethodNameCompleted event
-
Basically, this pattern enforces a pair of methods and an event to collaborate and help the application execute a thread asynchronously

Figure 1-3 Event Based Pattern
Task based Asynchronous Pattern
-
The Microsoft .NET Framework 4.0 introduces a new Task Parallel Library (TPL) for parallel computing and asynchronous programming. The namespace is "System.Threading.Tasks".
-
A Task can represent an asynchronous operation and a Task provides an abstraction over creating and pooling threads.







Jignesh KumarPosted Mar 7, 2020, 2:50 AM
Very informative article thank for sharing ..
Dipa MehtaPosted Mar 6, 2020, 7:45 AM
Very detailed article...
Md. KhanPosted Oct 29, 2018, 1:21 AM
Great explaining...
John SamuelPosted Sep 21, 2017, 7:02 AM
All your articles are very easy to understand. Great work. Thank You!!! :)
Ramesh beruguPosted Dec 2, 2014, 11:19 PM
in LoadCustomersAsync(), when control is reached cmd.ExecuteReaderAsync() method, the control will wait until it is completed, this is same like synchronous programming, here i could see the only one benefit that the UI will not blocked, can you please explain me if i am wrong
Vidya Vrat AgarwalPosted Sep 22, 2014, 4:25 PM
Code snippets are shown on the article.
Ramesh beruguPosted Sep 21, 2014, 11:44 AM
Sir, do you have sample code of this example
Vidya Vrat AgarwalPosted Aug 12, 2013, 8:29 AM
Glad that you guys find it helpful.
Ravi ShekharPosted Aug 12, 2013, 8:16 AM
Nice article sir. thank you for this article
Anurag SarkarPosted Aug 12, 2013, 5:44 AM
Thanks Sir.