Ads

C# Programming 28 - Demystifying Async and Await


In the dynamic world of C# programming, asynchronous programming has become increasingly essential for creating responsive and efficient applications. One powerful pair of keywords that plays a crucial role in asynchronous programming in C# is 'async' and 'await'. In this article, we will demystify these concepts and explore a real-world example to illustrate their practical application.



Understanding Async and Await:

Async and Await are keywords in C# that facilitate asynchronous programming, allowing developers to write code that can perform multiple tasks concurrently without blocking the main execution thread. This is particularly useful when dealing with time-consuming operations, such as network requests, file I/O, or database queries.

Async - The 'async' keyword is used to define methods as asynchronous. An asynchronous method can run independently of the main program execution, enabling other tasks to continue while the asynchronous operation is in progress.

Await - The 'await' keyword is used within an asynchronous method to pause its execution until the awaited task completes. This ensures that the asynchronous operation does not block the program flow, providing a non-blocking way to handle concurrent tasks.

Real-World Example: Fetching Data from an API

Let's delve into a practical scenario where async and await shine – fetching data from a remote API. Assume you have a requirement to retrieve information from a web service without freezing the user interface.

using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        await FetchDataAsync();
    }

    static async Task FetchDataAsync()
    {
        string apiUrl = "https://jsonplaceholder.typicode.com/todos/1";

        using (HttpClient client = new HttpClient())
        {
            try
            {
                HttpResponseMessage response = await client.GetAsync(apiUrl);

                if (response.IsSuccessStatusCode)
                {
                    string data = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Data from API: " + data);
                }
                else
                {
                    Console.WriteLine("Failed to retrieve data. Status code: " +
                    response.StatusCode);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }
        }
    }
}

Here is the output when we run the program



What we have done, here is the Explanation:

  • The Main method is marked as asynchronous with the 'async' keyword.
  • The FetchDataAsync method is also asynchronous, allowing it to perform non-blocking operations.
  • The HttpClient is used to make an asynchronous GET request to a sample API endpoint.
  • The 'await' keyword is used to wait for the completion of the asynchronous operations, preventing the program from being blocked during the HTTP request.

Here is the Another Example:

Certainly! Let's consider another real-world example where async/await can be beneficial: reading and processing data from multiple files concurrently. In this scenario, we'll create a simple program that reads text files asynchronously and counts the number of words in each file.

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

class WordCountApp
{
    static async Task Main()
    {
        Console.WriteLine("Welcome to the Word Count App!");

        // Input: Directory path containing text files
        Console.Write("Enter the directory path: ");
        string directoryPath = Console.ReadLine();

        // Get all text files in the specified directory
        string[] filePaths = Directory.GetFiles(directoryPath, "*.txt");

        // Process files concurrently
        await ProcessFilesAsync(filePaths);

        Console.WriteLine("Word count process completed. Press any key to exit.");
        Console.ReadKey();
    }

    static async Task ProcessFilesAsync(string[] filePaths)
    {
        // Create tasks to read and process each file concurrently
        Task<int>[] wordCountTasks = filePaths.Select(file => 
        CountWordsAsync(file)).ToArray();

        // Await the completion of all tasks
        int[] wordCounts = await Task.WhenAll(wordCountTasks);

        // Display the results
        for (int i = 0; i < filePaths.Length; i++)
        {
            Console.WriteLine($"{Path.GetFileName(filePaths[i])}: 
            {wordCounts[i]} words");
        }
    }

    static async Task<int> CountWordsAsync(string filePath)
    {
        try
        {
            string content = await File.ReadAllTextAsync(filePath);
            string[] words = content.Split(new[] { ' ', '\r', '\n', '\t' }, 
            StringSplitOptions.RemoveEmptyEntries);
            return words.Length;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error processing file {Path.GetFileName(filePath)}: 
            {ex.Message}");
            return 0;
        }
    }
}

In this example:

  • The Main method prompts the user to enter a directory path containing text files.
  • The ProcessFilesAsync method asynchronously processes each text file concurrently by creating tasks for counting words.
  • The CountWordsAsync method reads the content of a file asynchronously and counts the number of words.
  • The Task.WhenAll method is used to await the completion of all word-counting tasks concurrently.
  • The results are displayed, showing the word count for each processed file.

Finally:

Async and await in C# provide a powerful mechanism for handling asynchronous operations, ensuring responsiveness in your applications. By using these keywords, you can create efficient and scalable programs that can handle multiple tasks concurrently without sacrificing performance. The real-world example of fetching data from an API demonstrates the practical application of async and await, showcasing their importance in modern C# programming.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !