Learn C# · lesson 61 of 61 · about 12 minutes
Project: Download Queue
Run a set of simulated downloads at the same time and report on them.
What you'll learn
- Model downloads with records and an enum for results
- Start every download and await them together
- Report successes and failures with pattern matching
The dragon's hoard
At the back of the lair lies the dragon's hoard: a gold cup, a dragon egg, a crown of fire and a chest of coins (the list is given). The strongest adventurer can carry anything up to half their power, and every piece is carried out at once. It's the same job as a download queue: start every job, wait for them all, and report on each one, here gold cup: carried out (40 gold) or chest of coins: too heavy. Then the gold is counted, Carried out 3 of 4 pieces, 220 gold. Gold: 226, and the hero heads home: Ada heads home a dragon slayer., or, if the dragon fled, Ada heads home rich, but the dragon will be back.
The plan
Each line of input is a file and its size in KB, like photo.png 300. A pretend download takes a moment and then succeeds, or fails if the file is too big. The program will:
1. Write async Task<Result> DownloadAsync(string name, int kb)
2. Start every download, then await Task.WhenAll
3. Use a switch expression on each result to print done or failed
4. Print a summary with the totals
One download
Result r = await DownloadAsync("photo.png", 300);
Console.WriteLine($"{r.Name}: {r.Status}");
static async Task<Result> DownloadAsync(string name, int kb)
{
await Task.Delay(10);
return new Result(name, kb, kb <= 1000 ? Status.Done : Status.TooBig);
}
enum Status { Done, TooBig }
record Result(string Name, int Kb, Status Status);Output
photo.png: DoneReporting
A switch expression with a property pattern turns each result into a line: { Status: Status.Done } => .... LINQ's Count and Sum with a condition build the summary.
Many downloads at once
(string Name, int Kb)[] files = [("a.txt", 2), ("movie.mp4", 5000), ("b.png", 300)];
Result[] results = await Task.WhenAll(files.Select(f => DownloadAsync(f.Name, f.Kb)));
foreach (Result r in results)
{
Console.WriteLine(r switch
{
{ Status: Status.Done } => $"{r.Name}: done ({r.Kb} KB)",
_ => $"{r.Name}: failed, too big",
});
}
Console.WriteLine($"{results.Count(r => r.Status == Status.Done)} of {results.Length} done");
static async Task<Result> DownloadAsync(string name, int kb)
{
await Task.Delay(10);
return new Result(name, kb, kb <= 1000 ? Status.Done : Status.TooBig);
}
enum Status { Done, TooBig }
record Result(string Name, int Kb, Status Status);Output
a.txt: done (2 KB)
movie.mp4: failed, too big
b.png: done (300 KB)
2 of 3 doneCarrying the hoard out
party.OrderByDescending(a => a.Power()).First() picks the strongest adventurer, the first in the party's order if two are as strong, and their strength is half their power, so Bo, with 32, can carry 16. CarryAsync waits as long as the piece weighs and gives back a Trip that's Haul.Out or Haul.TooHeavy; a piece that weighs exactly the strength still gets out. hoard.Select(p => CarryAsync(p, strength)) starts every trip, and Task.WhenAll gives back a Trip[] in the hoard's order. The property pattern { Haul: Haul.Out } picks the line for each trip, and Where and Sum add up what got out before it goes into gold.
Tip
(string Name, int Kb) is a tuple with named parts: a quick way to group two values without writing a record.
Now try it yourself
This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.