How and when to use ‘async’ and ‘await’

前端 未结 21 1739
你的背包
你的背包 2020-11-21 05:07

From my understanding one of the main things that async and await do is to make code easy to write and read - but is using them equal to spawning background threads to perfo

21条回答
  •  臣服心动
    2020-11-21 05:27

    When using async and await the compiler generates a state machine in the background.

    Here's an example on which I hope I can explain some of the high-level details that are going on:

    public async Task MyMethodAsync()
    {
        Task longRunningTask = LongRunningOperationAsync();
        // independent work which doesn't need the result of LongRunningOperationAsync can be done here
    
        //and now we call await on the task 
        int result = await longRunningTask;
        //use the result 
        Console.WriteLine(result);
    }
    
    public async Task LongRunningOperationAsync() // assume we return an int from this long running operation 
    {
        await Task.Delay(1000); // 1 second delay
        return 1;
    }
    

    OK, so what happens here:

    1. Task longRunningTask = LongRunningOperationAsync(); starts executing LongRunningOperation

    2. Independent work is done on let's assume the Main Thread (Thread ID = 1) then await longRunningTask is reached.

      Now, if the longRunningTask hasn't finished and it is still running, MyMethodAsync() will return to its calling method, thus the main thread doesn't get blocked. When the longRunningTask is done then a thread from the ThreadPool (can be any thread) will return to MyMethodAsync() in its previous context and continue execution (in this case printing the result to the console).

    A second case would be that the longRunningTask has already finished its execution and the result is available. When reaching the await longRunningTask we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for the above example, where there's a Task.Delay(1000) involved.

提交回复
热议问题