You run the task in a synchounus manner. You can do something like this:
static async Task<int> CreateMultipleTasks()
{
var task1 = Task.Run<int>(() => WaitForMeAsync(5000));
var task2 = Task.Run<int>(() => WaitForMeAsync(3000));
var task3 = Task.Run<int>(() => WaitForMeAsync(4000));
Task.WaitAll(new Task[] { task1, task2, task3 });
return task1.Result + task2.Result + taks3.Result;
}
Using the three await
in a row will NOT run the tasks in parallel. It will just free the thread while it is waiting (if you use await Task.Delay(ms)
as Thread.Sleep(ms)
is a blocking operation), but the current execution will NOT continue with task2
while task1
is "sleeping".