C# What is objective and advantage of Async Await function

T.Zacks 3,986 Reputation points
2021-08-25T10:35:44.737+00:00

Anyone please post few sample code from where one can understand the advantages of using Async/Await function.

a) it is just only that UI will be responsive ?
b) current thread or main thread will not be blocked and can take more request

these are two benefit for which people use Async/Await function. ?

1) one sample code where Async/Await will not be used and post second one of same routine where Async/Await will be used to show the advantage of Async/Await.

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2021-08-25T22:29:22.56+00:00

    there are two uses of async/await

    1) do additional work while an async i/o operation is in progress. this can be extra computing or additional i/o

    2) manage parallel tasks via threads. this is helpful for cpu bound tasks, and allows more than one cpu processor to be used.

    1 person found this answer helpful.
    0 comments No comments

  2. Castorix31 83,206 Reputation points
    2021-08-25T10:41:08.407+00:00

    This article explains it well : Async and Await In C#
    or MSDN : Asynchronous programming with async and await

    0 comments No comments

  3. Karen Payne MVP 35,386 Reputation points
    2021-08-25T11:21:29.833+00:00

    Perhaps some solid code samples will assist in understanding Asynchronous programming.

    Then there is writing custom extensions

    namespace AsyncSimple.Classes
    {
        public static class Helpers
        {
            public static async IAsyncEnumerable<int> RangeAsync(this int start, int count, [EnumeratorCancellation] CancellationToken cancellationToken = default)
            {
    
                for (int index = 0; index < count; index++)
                {
                    if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested();
                    await Task.Delay(GlobalStuff.TimeSpan, cancellationToken);
                    yield return start + index;
                }
            }
    
        }
    }
    

    Or used when traversing a directory structure keeping the user interface responsive.

    namespace FileHelpers
    {
        public class Operations
        {
            public delegate void OnException(Exception exception);
            public static event OnException OnExceptionEvent;
    
            public delegate void OnUnauthorizedAccessException(string message);
            public static event OnUnauthorizedAccessException UnauthorizedAccessExceptionEvent;      
            public delegate void OnTraverseFolder(string status);
    
            public static event OnTraverseFolder OnTraverseEvent;
            public delegate void OnTraverseExcludeFolder(string sender);
            public static event OnTraverseExcludeFolder OnTraverseExcludeFolderEvent;       
            public static bool Cancelled = false;
            public static async Task RecursiveFolders(DirectoryInfo directoryInfo, string[] excludeFileExtensions, CancellationToken ct)
            {
    
                if (!directoryInfo.Exists)
                {
                    OnTraverseEvent?.Invoke("Nothing to process");
    
                    return;
                }
    
                if (!excludeFileExtensions.Any(directoryInfo.FullName.Contains))
                {
                    await Task.Delay(1, ct);
                    OnTraverseEvent?.Invoke(directoryInfo.FullName);
                }
                else
                {
                    OnTraverseExcludeFolderEvent?.Invoke(directoryInfo.FullName);
                }
    
                DirectoryInfo folder = null;
    
                try
                {
                    await Task.Run(async () =>
                    {
                                   foreach (DirectoryInfo dir in directoryInfo.EnumerateDirectories())
                                   {
    
                                       folder = dir;
    
                                       if ((folder.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden || 
                                           (folder.Attributes & FileAttributes.System) == FileAttributes.System ||
                                           (folder.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) {
                                           OnTraverseExcludeFolderEvent?.Invoke($"* {folder.FullName}");
    
                                           continue;
    
                                       }
    
                                       if (!Cancelled)
                                       {
    
                                           await Task.Delay(1);
                                           await RecursiveFolders(folder, excludeFileExtensions, ct);
    
                                       }
                                       else
                                       {
                                           return;
                                       }
    
                                       if (ct.IsCancellationRequested)
                                       {
                                           ct.ThrowIfCancellationRequested();
                                       }
    
                                   }
                    }, ct);
    
                }
                catch (Exception ex)
                {
                    if (ex is OperationCanceledException)
                    {
                        Cancelled = true;
                    }
                    else if (ex is UnauthorizedAccessException)
                    {
                        UnauthorizedAccessExceptionEvent?.Invoke($"Access denied '{ex.Message}'");
                    }
                    else
                    {
                        OnExceptionEvent?.Invoke(ex);
                    }
                }
            }
    
            public static void RecursiveFolders(string path, int indentLevel)
            {
    
                try
                {
    
                    if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                    {
    
                        foreach (string folder in Directory.GetDirectories(path))
                        {
                            Debug.WriteLine($"{new string(' ', indentLevel)}{Path.GetFileName(folder)}");
                            RecursiveFolders(folder, indentLevel + 2);
                        }
    
                    }
    
                }
                catch (UnauthorizedAccessException unauthorized)
                {
                    Debug.WriteLine($"{unauthorized.Message}");
                }
            }
    
        }
    
    }
    

  4. AgaveJoe 27,696 Reputation points
    2021-08-25T15:20:58.92+00:00

    Tell me please point wise the good side or advantage of Async / Await programming.

    A computer's internal hardware and APIs are all asynchronous. It makes sense to write programs that work with the hardware and APIs. That's where TAP comes into play. Task asynchronous programming model (TAP) is a C# programming pattern.

    It is important to understand that asynchronous programming has been around a very long time and writing asynchronous logic has always been the recommendation. The problems is asynchronous is a difficult concept to understand and previous asynchronous programming patterns closely mirrored an asynchronous transaction. These previous patterns require block of code to make a call and another block of code to handle the results; a callback. Understandably, two code blocks for every method call creates a complex code base that is difficult to maintain and debug. TAP solves the issues of previous C# asynchronous patterns by making the code appear synchronous to the developer while under the covers the compiler creates complex asynchronous code. The code is easier to write but TAP programming patterns must be followed along with a solid understanding of what asynchronous means. The links in this thread and your two duplicates threads illustrate these concepts.

    The key take away is always write asynchronous logic when executing API/hardware tasks that are asynchronous. These are non-cpu tasks like executing a SQL script or calling an HTTP service.

    a) it is just only that UI will be responsive ?

    That's a fairly narrow view. Asynchronous programming make more efficient use of system resources. Web developers also use the TAP pattern.

    b) current thread or main thread will not be blocked and can take more request

    That's the general idea. You get to squeeze the most performance out of a system.

    other than above 2 points what other reason exist for Async / Await programming ? can you please mention.

    Start by reading the links the community has provided in this thread and your duplicate posts.