2

If I have two Tasks that I am Awaiting:

await FirstTask();
await SecondTask();

Will the SecondTask only begin execution once FirstTask is complete?

Thanks

Scott
  • 83
  • 1
  • 5
  • 5
    Yes that is correct – Chronicle Nov 27 '20 at 15:29
  • 4
    Well, what did you find out while debugging your code? – devsmn Nov 27 '20 at 15:31
  • 3
    @RandRandom That's actually a pretty old, mostly unknown technique invented by Daniel Ebugging in the late 80's. Using that technique you can look through your code _step by step_ and see what's going on, that's crazy! – devsmn Nov 27 '20 at 15:44
  • Related: [Async/Await - is it *concurrent*?](https://stackoverflow.com/questions/7663101/async-await-is-it-concurrent) and [When I “await” an “async” method does it become synchronous?](https://stackoverflow.com/questions/32482275/when-i-await-an-async-method-does-it-become-synchronous) – Theodor Zoulias Nov 27 '20 at 23:17

1 Answers1

7

Yes that is what happens.

await FirstTask();
await SecondTask(); // doesn't start until FirstTask() finishes

If you want to have them run in parallel, and await both, you can use Task.WhenAll

var task1 = FirstTask();
var task2 = SecondTask(); // starts right away, even if FirstTask is still busy
await Task.WhenAll(task1, task2);
Chronicle
  • 1,565
  • 3
  • 22
  • 28