Search code examples
c#.netasync-awaitparallel-processing

C# Await/Async with a delegate vs direct await call - are the two approaches functionally equivalent?


Can anyone advise me if the two code snippets are functionally equivalent in .NET 8?

If not, why not?

FIRST SNIPPET


    Task<ICollection<SearchResults>> getResultTask = _aRepo.List(request.AParam, request.PageIndex, request.PageSize);

    if (request.PageIndex != null && request.PageSize != null)
    {
       int totalRecords = await _myRepo.CountAsync();
       // store totalrecords (non-async)
    }


    ICollection<SearchResults> results = await getResultTask;

SECOND SNIPPET:

    if (request.PageIndex != null && request.PageSize != null)
    {
       int totalRecords = await _myRepo.CountAsync();
       // store totalrecords (non-async)
    }

    // Is this functionally equivalent to the above?
    ICollection<SearchResults> results = await _aRepo.List(request.AParam, request.PageIndex, request.PageSize);

Solution

  • Consider the following code:

    static async Task method1()
    {
        await Task.Delay(2000);
    }
    
    static async Task method2()
    {
        await Task.Delay(3000);
    }
    

    Now if you call that from a console app like so:

    public static async Task Main()
    {
        var sw = Stopwatch.StartNew();
    
        var task1 = method1(); // Starts method1() without waiting for it to return.
    
        await method2();  // Starts method2() and waits for it to return.
        await task1;      // Wait for method1() to return.
    
        Console.WriteLine(sw.Elapsed); // Will be approximately 3s - the longer of the two method times.
        Console.ReadLine();
    }
    

    This will output a time of around 3s, because method1() has been started AND NOT AWAITED before method2() is started and awaited.

    method1() will take around 2s and method2() will take around 3s, so the overall time will be the longer of those two times.

    Now see what happens if you do this:

    public static async Task Main()
    {
        var sw = Stopwatch.StartNew();
    
        await method1(); // Starts method1() and waits for it to return.
        await method2(); // Starts method2() and waits for it to return.
    
        Console.WriteLine(sw.Elapsed); // Will be approximately 5s - the sum of the two method times.
        Console.ReadLine();
    }
    

    This will output around 5s, because the method1() is started and awaited, taking around 2s to complete. Only after that is method2() started and awaited, which takes around 3s to complete.

    Therefore the overall time is 5s (as compared to 3s for the first example).