Skip to content

Commit

Permalink
Update chapter04.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cwoodruff committed Mar 21, 2024
1 parent e5035ed commit 8617fff
Showing 1 changed file with 78 additions and 28 deletions.
106 changes: 78 additions & 28 deletions Chapter04/chapter04.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,62 +92,112 @@ Imagine the async keyword in C# as a friendly magician in your coding toolbox. W
Without async (the world without magic):

```C#
public string DownloadContent()
using System;
using System.Threading;

public class BeforeAsync
{
using var client = new HttpClient();
// This is like waiting for a snail to deliver your mail.
string result = client.GetStringAsync("http://example.com").Result;
return result;
public int CalculateSum()
{
Thread.Sleep(5000); // Pretending to crunch some serious numbers for 5 seconds.
return 42; // The universe's answer to everything, obviously.
}

public void ShowSum()
{
int sum = CalculateSum(); // Waiting...waiting...
Console.WriteLine($"Calculated Sum: {sum}");
}
}

// Usage:
var beforeAsync = new BeforeAsync();
beforeAsync.ShowSum(); // Time seems to stand still...
```

In this mundane version, your code sits idly by, twiddling its virtual thumbs, waiting for the data to download before it can move on to the next line.
Here, your program is like a patient in a doctor's waiting room, just sitting and twiddling its thumbs until the CalculateSum method graciously decides to return.

With async (welcome to the magic show!):

```C#
public async Task<string> DownloadContentAsync()
using System;
using System.Threading.Tasks;

public class AfterAsync
{
using var client = new HttpClient();
// Now, our hero can zip off and do other things while waiting for the signal.
string result = await client.GetStringAsync("http://example.com");
return result; // Ta-da! The content arrives without any drama.
public async Task<int> CalculateSumAsync()
{
await Task.Delay(5000); // Simulates crunching numbers asynchronously.
return 42; // Still the answer to everything, but faster!
}

public async Task ShowSumAsync()
{
int sum = await CalculateSumAsync(); // Look at me! I can do other things while waiting!
Console.WriteLine($"Calculated Sum: {sum}");
}
}

// Usage:
var afterAsync = new AfterAsync();
await afterAsync.ShowSumAsync(); // Whoosh! Off to multitask-land!
```
In the async version, your code requests the data and then gets back to other tasks, not wasting a single moment. When the data is finally ready, it picks up right where it left off.

In this exhilarating async universe, your program can now send off the CalculateSumAsync method on its merry way and get on with other tasks instead of sitting idly by. When the method is done, it'll nudge your program, saying, "Hey, I’ve got your answer!" This is efficiency at its finest, turning your code from a snooze fest into a productivity powerhouse. Welcome to the future, where your code doesn't just run; it sprints.

### The await keyword

Picture the await keyword in C# as your trusty time-traveling sidekick in the vast universe of code. While the rest of your code marches on a linear path, facing delays and waiting periods, await zips through the time-space continuum, allowing your program to leap forward and tackle other tasks instead of twiddling its digital thumbs. It's like having a superpower that enables you to avoid the coffee line in the morning rush - you place your order (start an async operation), then use your powers (await) to pause time, do a day's work, and return just as your coffee is ready.
Dive into the fantastic world of the await modifier in C# 10 and .NET 6, where your code dances through tasks with the grace of a ballet star, skipping over the boring waits and making efficiency look effortless. Imagine await as the magical word that pauses your favorite TV show (a task) while you run to grab a snack, ensuring you don't miss a single moment. It tells your program, "Hey, chill for a sec, I've got some stuff to handle, but don't worry, you won't even notice I'm gone!"

Without await (stuck in line):
Before the era of await, here's how our code drama unfolded:

```C#
public string MakeBreakfast()
public class NoAwaitLand
{
// Imagine this is a method to toast your bread, taking 5 minutes
string toast = MakeToast();
return toast; // You're just standing there, watching the toaster.
public string MakeTea()
{
Thread.Sleep(3000); // Simulating the time it takes to make tea.
return "Tea is ready!";
}

public void EnjoyTea()
{
string tea = MakeTea(); // Time drags... and drags...
Console.WriteLine(tea); // Finally, tea time, but your show is over!
}
}

// Usage:
var noAwaitLand = new NoAwaitLand();
noAwaitLand.EnjoyTea(); // Sigh... Waiting is such sweet sorrow.
```

Here, your code is as stuck as you are in the slow-moving queue, waiting for the toast to pop before moving on to anything else.
In this world, making tea means missing the crucial plot twists of your show. Your code, like you, sits and waits, wasting precious moments.

With await (time-traveling while you wait):
Now, let's sprinkle some await magic into the mix:

```C#
public async Task<string> MakeBreakfastAsync()
public class AwaitLand
{
// Now, this is an asynchronous method to toast your bread
Task<string> toastTask = MakeToastAsync();
// Go on with your morning routine, no need to watch the bread brown!
string coffee = MakeCoffee();
string toast = await toastTask; // Come back just in time for the toast!
return toast + " and " + coffee; // Breakfast is ready, without the wait!
public async Task<string> MakeTeaAsync()
{
await Task.Delay(3000); // Like putting the kettle on and walking away.
return "Tea is ready, and you didn't miss a thing!";
}

public async Task EnjoyTeaAsync()
{
string tea = await MakeTeaAsync(); // The magic happens: pause and resume!
Console.WriteLine(tea); // Tea time aligns with your show's commercial break!
}
}

// Usage:
var awaitLand = new AwaitLand();
await awaitLand.EnjoyTeaAsync(); // Look at you, multitasking like a pro!
```

In this scenario, while the toast is getting ready, you're not standing idle; you're making coffee, flipping through the newspaper, or maybe even planning your day. The await keyword makes your code just as efficient, allowing other operations to run while it awaits the completion of the asynchronous task, without blocking the entire process.
With the power of await, your code now makes tea while still keeping up with every twist and turn of your TV show. No more missed moments, no more waiting around—just pure, uninterrupted productivity. The await modifier turns your tasks into background ninjas, completing their missions without pulling you away from what matters. Welcome to the future, where your code keeps up with your life's pace, making every second count!

# Strategies for writing asynchronous code

Expand Down

0 comments on commit 8617fff

Please sign in to comment.