blog

Home / DeveloperSection / Blogs / Asynchronous programming with async, await, Task in C#

Asynchronous programming with async, await, Task in C#

Asynchronous programming with async, await, Task in C#

Ravi Vishwakarma 105 10-Jun-2024

Asynchronous programming in C# using async and await is a powerful feature that allows you to write non-blocking code, making your applications more responsive and efficient. 

Here's a basic overview of asynchronous programming with async, await, and Task:

Async and Await Keywords:

  1. async: This keyword is used to define an asynchronous method. An asynchronous method can contain one or more await expressions.
  2. await: This keyword is used to asynchronously wait for a task to complete. It allows the method to pause execution until the awaited task is finished without blocking the calling thread.

Task Class:

  1. Task: Represents an asynchronous operation that can return a result. It is a fundamental type used in asynchronous programming in C#. Tasks can be awaited using the await keyword.
  2. Task<T>: Represents an asynchronous operation that returns a result of type T.

Creating Asynchronous Methods:

  1. To create an asynchronous method, mark the method with the async keyword in its signature.
  2. Inside the method, use the await keyword before calling asynchronous operations, such as I/O operations or CPU-bound computations.

Example:

using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main()
        {
            // Asynchronously call the MyMethodAsync method
            string result = (string) await MyMethodAsync();
            Console.WriteLine(result);
            Console.ReadLine();
        }

        static async Task<object> MyMethodAsync()
        {
            // Simulate an asynchronous operation
            await Task.Delay(1000); // Asynchronous delay for 1 second
            return "Async operation completed!";
        }
    }
}

Error Handling:

  1. Use try-catch blocks to handle exceptions in asynchronous methods.
  2. Exceptions thrown in asynchronous tasks are propagated when the task is awaited.

Async Task with Return Value:

  1. To return a value from an asynchronous method, specify the return type as Task<T> where T is the type of the result.
  2. Use the return keyword to return the result from the asynchronous method.

Benefits:

  1. Improves application responsiveness by allowing the UI thread to remain unblocked during long-running operations.
  2. Utilizes system resources more efficiently by allowing other tasks to execute while waiting for I/O operations.

Async and await in C# simplify asynchronous programming by providing an intuitive syntax while retaining the power and flexibility to handle complex asynchronous workflows.


Updated 10-Jun-2024
Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur. SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer

Leave Comment

Comments

Liked By