Different behavior async/await in almost the same methods

后端 未结 4 1623
粉色の甜心
粉色の甜心 2021-01-17 09:59

Let\'s say I have two async methods

public async static Task RunAsync1()
{
    await Task.Delay(2000);
    await Task.Delay(2000);
}

and

4条回答
  •  别那么骄傲
    2021-01-17 10:23

    Whenever you start a Task. It already started when you created it, not when you called await.

    If you create a task and put it in a variable, it might already finish when you await that. This is what happen to your second case. await just ensures that it must finish before continuing.

提交回复
热议问题