0
0
C Sharp (C#)programming~10 mins

Task.WhenAll for parallel execution in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run two tasks in parallel and wait for both to finish.

C Sharp (C#)
var task1 = Task.Run(() => Console.WriteLine("Task 1"));
var task2 = Task.Run(() => Console.WriteLine("Task 2"));
await [1](task1, task2);
Drag options to blanks, or click blank then click option'
ATask.WhenAll
BTask.WaitAll
CTask.RunAll
DTask.WaitAny
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which is synchronous and blocks the thread.
Using Task.WaitAny which waits for only one task to complete.
2fill in blank
medium

Complete the code to start three tasks and wait for all to complete using Task.WhenAll.

C Sharp (C#)
var tasks = new List<Task> {
    Task.Run(() => DoWork(1)),
    Task.Run(() => DoWork(2)),
    Task.Run(() => DoWork(3))
};
await [1](tasks);
Drag options to blanks, or click blank then click option'
ATask.WaitAll
BTask.WhenAll
CTask.RunAll
DTask.WaitAny
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the list to Task.WaitAll which is synchronous.
Using Task.WaitAny which waits for only one task.
3fill in blank
hard

Fix the error in the code to correctly wait for multiple tasks to complete asynchronously.

C Sharp (C#)
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");
Drag options to blanks, or click blank then click option'
Aawait Task.WhenAny(taskA, taskB);
BTask.WaitAll(taskA, taskB);
CTask.WhenAny(taskA, taskB);
Dawait Task.WhenAll(taskA, taskB);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which blocks the thread.
Using Task.WhenAny which waits for only one task.
4fill in blank
hard

Fill both blanks to create a dictionary with task results after all tasks complete.

C Sharp (C#)
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);
Drag options to blanks, or click blank then click option'
ATask.WhenAll
BSelect
CSelectIndexed
DSelectMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using SelectMany which flattens collections incorrectly.
Using a non-existent method SelectIndexed.
5fill in blank
hard

Fill in the blanks to run tasks in parallel, filter results greater than 5, and create a list of those results.

C Sharp (C#)
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));
Drag options to blanks, or click blank then click option'
ATask.WhenAll
B>
C<
DTask.WaitAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which blocks the thread instead of awaiting.
Using '<' which filters results less than 5.