Complete the code to run two tasks in parallel and wait for both to finish.
var task1 = Task.Run(() => Console.WriteLine("Task 1")); var task2 = Task.Run(() => Console.WriteLine("Task 2")); await [1](task1, task2);
Complete the code to start three tasks and wait for all to complete using Task.WhenAll.
var tasks = new List<Task> {
Task.Run(() => DoWork(1)),
Task.Run(() => DoWork(2)),
Task.Run(() => DoWork(3))
};
await [1](tasks);Fix the error in the code to correctly wait for multiple tasks to complete asynchronously.
var taskA = Task.Run(() => Compute(10)); var taskB = Task.Run(() => Compute(20)); Task.WhenAll(taskA, taskB).Wait(); // Fix this line Console.WriteLine("All tasks done");
Fill both blanks to create a dictionary with task results after all tasks complete.
var tasks = new List<Task<int>> {
Task.Run(() => 1),
Task.Run(() => 2),
Task.Run(() => 3)
};
var results = await [1](tasks);
var dict = results.[2]((result, index) => new { index, result }).ToDictionary(x => x.index, x => x.result);Fill in the blanks to run tasks in parallel, filter results greater than 5, and create a list of those results.
var tasks = new List<Task<int>> {
Task.Run(() => 3),
Task.Run(() => 7),
Task.Run(() => 10)
};
var results = await [1](tasks);
var filtered = results.Where(x => x [2] 5).ToList();
Console.WriteLine(string.Join(", ", filtered));