Await before an uncompleted Task will pass the control to the caller, until the Task is completed.
When you use it in Main() who is going to get the control?
public static async Task Main()
{
await F1() ; //This await will pass the control to ???
}
public static async Task F1()
{
await Task.Run(()=>{...}) ; //This await will pass the control to Main()
}
The primary thread that keeps your app alive is effectively:
private static void TheRealEntryPoint() => Main().GetAwaiter().GetResult();
(which is broadly the same as .Wait()
, and is the kind of "sync-over-async" thing that you should never write in your own code, but ... which suffices in this specific scenario)
As such:
Task.Run
returns an incomplete taskawait
on that returns to the caller, taking us back to Main()
await
also returns to the caller - so we end up in TheRealEntryPoint
F1
, which can now mark itself as completeMain
, which can now mark itself as completeTheRealEntryPoint
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With