I have seen several posts on this topic but the answers are complicated. Suppose I have the following code:
static async Task Main(string[] args)
{
await Task.Run(LongRunningTask1);
await LongRunningTask2();
await Task.Run(LongRunningTask2);
Console.ReadLine();
}
static void LongRunningTask1()
{
Thread.Sleep(2000);
}
static async Task LongRunningTask2()
{
Thread.Sleep(2000);
}
Console.ReadLine();
Each of the above await expressions does the same thing.
Can someone give me a simplified summary of when you would use each one?
Also, which of the three await expressions create a separate thread? I assume that it is the first and third. If the second uses only the UI thread, then how is the execution time apportioned between Main() and the awaited task?
I have read that the second choice slows down the GUI (if, for example, Main() were instead a button click handler for a WinForms program). Then why use the second choice? What does it buy me?