CodeBlanks Logo Icon CodeBlanks

Summary - Task and ContinueWith

C# course · Advanced · Lesson 12/83

Combine WhenAll, WhenAny and CancellationToken to write a parallel downloader.

Summary - Task and ContinueWith

In this group you learned the basics of Task operations: Task.Run sends work to the pool; ContinueWith registers a continuation; Task.WhenAll waits for all Tasks; Task.WhenAny waits for the first; CancellationToken cancels work from outside.

The key tip: in new code, await is usually cleaner than ContinueWith. For cancellation use ThrowIfCancellationRequested. Now we will combine all of this in a small app.

Exercise Combine two methods with WhenAll

Define static Task<int> GetA() (return Task.FromResult(10);). Define static Task<int> GetB() (return Task.FromResult(20);). Make Main a static async Task Main. Await with int[] arr = await Task.WhenAll(GetA(), GetB()); and print the sum with Console.WriteLine(arr[0] + arr[1]);.

Show example solution
return Task.FromResult(10); and return Task.FromResult(20);. Make Main static async Task Main() and write int[] arr = await Task.WhenAll(GetA(), GetB()); Console.WriteLine(arr[0] + arr[1]);
Example solution:
using System;
using System.Threading.Tasks;

class Program
{
    static Task<int> GetA()
    {
        return Task.FromResult(10);
    }

    static Task<int> GetB()
    {
        return Task.FromResult(20);
    }

    static async Task Main()
    {
        int[] arr = await Task.WhenAll(GetA(), GetB());
        Console.WriteLine(arr[0] + arr[1]);
    }
}

Tips

A few tips: Task.Run only for CPU work; for I/O call an async method directly. Prefer await over ContinueWith. Use WhenAll to wait for many Tasks, WhenAny for races.

When writing cancellable methods, always take a CancellationToken parameter and call ThrowIfCancellationRequested inside loops. Cancellation is not an error, it is an expected flow.

Exercise Mini app: Parallel squares

Define static Task<int> Compute(int x) (return Task.FromResult(x * x);). Make Main a static async Task Main. Compute two squares in parallel with int[] arr = await Task.WhenAll(Compute(3), Compute(4)); and print the sum with Console.WriteLine(arr[0] + arr[1]); (3² + 4² = 9 + 16 = 25).

Show example solution
In Compute body write return Task.FromResult(x * x);. Make Main static async Task Main() and write int[] arr = await Task.WhenAll(Compute(3), Compute(4)); Console.WriteLine(arr[0] + arr[1]);
Example solution:
using System;
using System.Threading.Tasks;

class Program
{
    static Task<int> Compute(int x)
    {
        return Task.FromResult(x * x);
    }

    static async Task Main()
    {
        int[] arr = await Task.WhenAll(Compute(3), Compute(4));
        Console.WriteLine(arr[0] + arr[1]);
    }
}

Test yourself

  1. What does Task.WhenAll do?

    • Waits for all Tasks
    • Waits for the first Task
    • Cancels the Tasks
    • Runs the Tasks one by one
    Show answer

    Waits for all Tasks — Correct. WhenAll waits until all Tasks finish.

All lessons in Advanced