Yes, Task.WhenAll can execute the tasks in parallel. It waits for all the provided tasks to complete, and those tasks can run concurrently. In the example provided, the two Task.Run calls create tasks that will run in parallel, assuming the environment (e.g., the thread pool) allows it.
Here’s the modified code again for clarity:
XLWorkbook FinalWorkbook = null;
XLWorkbook FinalWorkbook_Win = null;
// Create two tasks that run in parallel
var task1 = Task.Run(() =>
progress.Report("Task 1/8 : Loading Main Workbook for Mac...");
// Create XLWorkbook Object in Memory from Master file
return new XLWorkbook(parentMemStream_mac);
var task2 = Task.Run(() =>
progress.Report("Task 1/8 : Loading Main Workbook for Windows...");
// Create XLWorkbook Object in Memory from Master file
return new XLWorkbook(parentMemStream_win);
// Wait for both tasks to complete
await Task.WhenAll(task1, task2);
// Assign results to variables
FinalWorkbook = task1.Result;
FinalWorkbook_Win = task2.Result;
Here’s what happens:
Task.Run queues the specified actions to run on the thread pool, potentially on separate threads.
- Both tasks (
task1 and task2) are started and may run in parallel, depending on available resources.
Task.WhenAll waits for both tasks to complete without blocking the main thread.
- Once both tasks complete, their results are assigned to
FinalWorkbook and FinalWorkbook_Win.
The parallel execution is handled by the task scheduler, which distributes the work to available threads. If the environment has enough resources (like CPU cores), the tasks will indeed run in parallel.