I’m starting to learn about async / await in C# 5.0, and I don’t understand it at all. I don’t understand how it can be used for parallelism. I’ve tried the following very basic program:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Task task1 = Task1();
Task task2 = Task2();
Task.WaitAll(task1, task2);
Debug.WriteLine("Finished main method");
}
public static async Task Task1()
{
await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(5)));
Debug.WriteLine("Finished Task1");
}
public static async Task Task2()
{
await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(10)));
Debug.WriteLine("Finished Task2");
}
}
}
This program just blocks on the call to Task.WaitAll() and never finishes, but I am not understanding why. I’m sure I’m just missing something simple or just don’t have the right mental model of this, and none of the blogs or MSDN articles that are out there are helping.
I recommend you start out with my intro to
async/awaitand follow-up with the official Microsoft documentation on TAP.As I mention in my intro blog post, there are several
Taskmembers that are holdovers from the TPL and have no use in pureasynccode.new TaskandTask.Startshould be replaced withTask.Run(orTaskFactory.StartNew). Similarly,Thread.Sleepshould be replaced withTask.Delay.Finally, I recommend that you do not use
Task.WaitAll; your Console app should justWaiton a singleTaskwhich usesTask.WhenAll. With all these changes, your code would look like: