Async Methods and Awaiting Tasks in C#
Asynchronous programming in the C# programming language is a programming concept or feature that allows users to perform long-running programming operations without blocking the program’s main thread. Asynchronous programming in C# improves the responsiveness and performance of your programs and applications on multiple device systems.

Keywords used in asynchronous programming in C#.
- async
- await
The asynchronous programming keywords defined above are commonly used with Task and Task<T>.
What is an Async Method in C#?
An async method in C# programming is a programming method or concept that executes or runs an asynchronous task process using the async keyword.
It allows a program to continue executing other program tasks while waiting for a task to complete.
Syntax of Async Method.
async Task MethodName()
{
await AnyTask();
}
What is await in C#?
In C# asynchronous programming, the await keyword stops or holds the execution of an async method until the current program event or the asynchronous task being held or attended to terminates.
Key points about the await keyword.
- It does not block the main thread in the current program.
- It returns current control back to the caller.
- Program execution resumes after the asynchronous task in the application completes.
Why use Async and Await in C#?
- Async programming in C# is useful for these main tasks:
- Processing file handling operations in C#.
- Creating existing file database calls.
- In managing large-scale web-based API requests.
- In heavy-traffic network communication establishments.
- In long-running program calculations.
Without asynchronous programming in C#, your program or application may freeze while waiting for a program operation task to complete.
Example of Async and Await in C#.
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
Console.WriteLine(“Start Task”);
await UploadFile();
Console.WriteLine(“Task Done”);
}
static async Task UploadFile()
{
Console.WriteLine(“Uploading Start…”);
// here it simulates a 5-second delay for the task
await Task.Delay(5000);
Console.WriteLine(“Uploading File Terminate”);
}
}
Explaining Async and Await in C#.
- In this program, the Main() function method is indicated by async.
- In the program, UploadFile() provides a task.
- In which the await Task.Delay(5000); method pauses or delays the current task for 5 seconds.
- The thread is not blocked during the task wait period.
- After the task completes, program execution continues normally.
Returning a Value from an Async Method in C#.
You can return a program value by invoking the async method Task<T> in a C# program.
Example of Returning a Value from an Async Method.
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
int output = await TotalIntegers();
Console.WriteLine(“Result = ” + output);
}
static async Task<int> TotalIntegers()
{
await Task.Delay(4000);
return 3 + 7; result – 10 (after 4 seconds)
}
}
Types of async return values in a C# program.
- This user-defined program provides a description of the parameter value return type.
- Here, Task does not accept any return value.
- In which Task<T> provides a value.
- The void keyword is mostly used for event handlers as a return value.
Difference between synchronous and asynchronous execution in C#.
Synchronous execution.
- In this, program event tasks are executed one after another.
- In this, the next task in the current program or the previous program task waits until the end of the program.
- It can block the current program or application.
Asynchronous execution.
- In this, program and application tasks are executed without blocking.
- It improves the responsiveness of the system.
- It is a better option for I/O operations in the system.
Example of multiple asynchronous tasks.
Using System;
using System.Threading.Tasks;
class program
{
static async Task Main()
{
Task taskfirst = Task1();
Task tasksecond = Task2();
Task taskthird = Task3();
await taskfirst;
wait tasksecond;
wait task third;
Console.WriteLine(“All Tasks Terminated”);
}
static async Task Task1()
{
await Task.Delay(3000);
Console.WriteLine(“Task First Done”);
}
static async Task Task2()
{
await Task.Delay(2000);
Console.WriteLine(“Task Second Done”);
}
static async Task Task3()
{
await Task.Delay(1000);
Console.WriteLine(“Task Third Done”);
}
}
Advantages of the Async/Await concept in C#.
- It keeps the application UI responsive in your program.
- Improves the performance of your program or application.
- Helps create asynchronous source code in your program.
- This program is easier and simpler to read than callbacks.
- It has better scalability and improvement features for web server applications.
Important things about asynchronous programming.
Program-created async methods normally return Task output.
async Task TestMethod()
Use the await keyword inside async methods.
await Task.Delay(5000);
How to avoid blocking calls in your program.
Avoid blocking calls.
Task.Delay(2000).Wait();
Use this for blocking calls.
await Task.Delay(4000);
Real-life example of asynchronous programming.
Suppose you are waiting in a queue or line for a health checkup at a hospital.
- Synchronous operation – Here you stand at the medical counter and wait with your health checkup slip.
- Asynchronous operation – Here you wait for your turn in line for the health checkup according to your slip number, and sit or stand at your table bench while waiting for your turn
Async programming allows the program to continue performing other secondary tasks while waiting.
Async Methods and Awaiting Tasks Summary.
In C# programming, the Async method and the await, Task, and Task<T> keywords are used to perform non-blocking asynchronous program task operations. Asynchronous program tasks help you keep your program or application responsive and efficient when performing long-running programming tasks such as file handling activities, database manipulation operations, or web API requests.
