0

I'm trying to understand async/await in C#. I have two methods here to explain. With Method1 I understand that the creation of the Task begins the execution of the LongRunningOperationAsync(), and now we can do other things before awaiting the result, so two things are happening at the same time. In the real world I see a lot of code like Method2, and I cannot see the advantage of using async/await in this case. I've done a lot of reading but it's just not clicking. Please help.

class Program
{
    public static async Task Main()
    {
        await MyMethodAsync1();
        //await MyMethodAsync2();
    }

    public static async Task MyMethodAsync1()
    {
        Task<int> longRunningTask = LongRunningOperationAsync();
        // long op method is now running so can do independent work
        // which doesn't need the result of long op
        await Task.Delay(1000);

        int result = await longRunningTask;
        Console.WriteLine("MyMethodAsync1() finished");
    }

    // This method just awaits the long op method directly,
    // so doesn't seem to have any benefit
    public static async Task MyMethodAsync2()
    {
        int result = await LongRunningOperationAsync();
        Console.WriteLine("MyMethodAsync2() finished");
    }

    public static async Task<int> LongRunningOperationAsync()
    {
        Console.WriteLine("LongRunningOperationAsync() started");
        await Task.Delay(2000);
        Console.WriteLine("LongRunningOperationAsync() finished");
        return 1;
    }
}

Bobbler
  • 633
  • 1
  • 7
  • 21
  • 2
    TL;DR: in the time it's awaiting, the thread can _do other things_ – Franz Gleichmann Jun 14 '22 at 09:40
  • "so doesn't seem to have any benefit" without context it really has no benefit. But if this would be called in a UI context, then this would ensure that the UI does not freeze for example – Mong Zhu Jun 14 '22 at 09:43
  • Not sure if that is your question, but Stephen C has to say about [Eliding Async and Await](https://blog.stephencleary.com/2016/12/eliding-async-await.html). – Fildor Jun 14 '22 at 09:52
  • 1
    On second glance at the question: It may become clearer if you realize that the main goal of async/await is _not_ concurrency but asyncrony. You _can_ get concurrency as a sideeffect especially if you look at CPU-Bound async operations. – Fildor Jun 14 '22 at 09:57
  • 1
    See what you've done with `LongRunningOperationAsync` inside `MyMethodAsync1`? Using `await` inside `MyMethodAsync2` allows the caller of `MyMethodAsync2` to do something similar. – Johnathan Barclay Jun 14 '22 at 09:59
  • please try to formulate a precise question, otherwise we end up in a long comments conversation without any profound answer that might help future visitors, and we all know that this usually results in closing the question as unclear or something alike – Mong Zhu Jun 14 '22 at 10:04
  • You'd better compare with APM and EAP. The benefit of TAP is less methods and makes your code clean. – shingo Jun 14 '22 at 10:13
  • If you think that the `MyMethodAsync2` doesn't have to be `async`, then please show us how you can rewrite the same method in a way that does exactly the same job, and it's not `async`. – Theodor Zoulias Jun 14 '22 at 10:33

0 Answers0