Skip to content

Commit

Permalink
added chapter 4 sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
cwoodruff committed May 24, 2024
1 parent a36b9e7 commit e5c7f72
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Ch04/ch04.sln
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csharp-async-example", "csharp-async-example\csharp-async-example.csproj", "{0CC619F2-A17E-46DE-9CCD-BA51E9C145F9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0CC619F2-A17E-46DE-9CCD-BA51E9C145F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CC619F2-A17E-46DE-9CCD-BA51E9C145F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CC619F2-A17E-46DE-9CCD-BA51E9C145F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CC619F2-A17E-46DE-9CCD-BA51E9C145F9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
83 changes: 83 additions & 0 deletions src/Ch04/csharp-async-example/AsynchronousChef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace csharp_async_example;

public class AsynchronousChef
{
private int DelayMultiply { get; set; } = 100;

public async Task MakeBreakfast()
{
var waterTask = BoilWater();
var eggsTask = BoilEggs();
var baconTask = FryBacon();
var breadTask = ToastBread();

await breadTask;
await ApplyButter();
await ApplyJam();

await Task.WhenAll(eggsTask, baconTask, waterTask);
await PourCoffee();
await PourJuice();
}

private async Task PourCoffee()
{
Console.WriteLine("[Chef] Start pouring coffee");
await Task.Delay(5 * DelayMultiply);
Console.WriteLine("[Chef] Coffee Ready");
}

private async Task BoilEggs()
{
Console.WriteLine("[Chef] Put Eggs into boiling water");
await Task.Delay(70 * DelayMultiply);
Console.WriteLine("[Chef] Eggs Boiled");
}

private async Task FryBacon()
{
Console.WriteLine("[Chef] Throw bacon in pan");
await Task.Delay(40 * DelayMultiply);
Console.WriteLine("[Chef] Bacon Fried");
}

private async Task ToastBread()
{
Console.WriteLine("[Chef] Put bread in toaster");
await Task.Delay(20 * DelayMultiply);
Console.WriteLine("[Chef] Bread Toasted");
}

private async Task ApplyButter()
{
Console.WriteLine("[Chef] Start spreading Butter");
await Task.Delay(15 * DelayMultiply);
Console.WriteLine("[Chef] Butter applied");
}

private async Task ApplyJam()
{
Console.WriteLine("[Chef] Start spreading Jam");
await Task.Delay(15 * DelayMultiply);
Console.WriteLine("[Chef] Jam applied");
}

public async Task PourJuice()
{
Console.WriteLine("[Chef] Start pouring Juice");
await Task.Delay(5 * DelayMultiply);
Console.WriteLine("[Chef] Juice Ready");
}

private async Task BoilWater()
{
Console.WriteLine("[Chef] Set coffee water to boil");
await Task.Delay(200 * DelayMultiply);
Console.WriteLine("[Chef] Coffee Water is boiled");
}

public async Task<string> TellAJoke()
{
return "Joke";
}
}
53 changes: 53 additions & 0 deletions src/Ch04/csharp-async-example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Diagnostics;

namespace csharp_async_example;

public abstract class Program
{
private static Stopwatch? Stopwatch { get; set; }

static void Main(string[] args)
{
Stopwatch = new Stopwatch();
Stopwatch.Start();
Console.WriteLine($"[Kitchen] Started making breakfast - Synchronous Chef");

MakeBreakfastWithSynchronousChef();

Console.WriteLine($"[Kitchen] Breakfast done in '{Stopwatch.Elapsed.Seconds}'s '{Stopwatch.Elapsed.Milliseconds}'ms");
Stopwatch.Restart();

Console.WriteLine("---");
Console.WriteLine($"[Kitchen] Started making breakfast - Asynchronous Chef");

MakeBreakfastWithAsynchronousChef().Wait();

Console.WriteLine($"[Kitchen] Breakfast done in '{Stopwatch.Elapsed.Seconds}'s '{Stopwatch.Elapsed.Milliseconds}'ms");
}

static void MakeBreakfastWithSynchronousChef()
{
var chef = new SynchronousChef();
chef.MakeBreakfast();
}

static async Task MakeBreakfastWithAsynchronousChef()
{
var chef = new AsynchronousChef();
await chef.MakeBreakfast();
}

static async Task MakeBreakfastAndDisturbChef()
{
var chef = new AsynchronousChef();
var breakfastTask = chef.MakeBreakfast();

Task.Delay(1500).Wait();
Console.WriteLine($"[Kitchen] a joke from chef: '{chef.TellAJoke().Result}'");

Task.Delay(9000).Wait();
chef.PourJuice().Wait();

await breakfastTask;
}
}
74 changes: 74 additions & 0 deletions src/Ch04/csharp-async-example/SynchronousChef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace csharp_async_example;

public class SynchronousChef
{
private int DelayMultiply { get; set; } = 100;

public void MakeBreakfast()
{
BoilWater();
BoilEggs();
FryBacon();
ToastBread();
ApplyButter();
ApplyJam();
PourCoffee();
PourJuice();
}

private void PourCoffee()
{
Console.WriteLine("[Chef] Start pouring coffee");
Thread.Sleep(5 * DelayMultiply);
Console.WriteLine("[Chef] Coffee Ready");
}

private void BoilEggs()
{
Console.WriteLine("[Chef] Put Eggs into boiling water");
Thread.Sleep(70 * DelayMultiply);
Console.WriteLine("[Chef] Eggs Boiled");
}

private void FryBacon()
{
Console.WriteLine("[Chef] Throw bacon in pan");
Thread.Sleep(40 * DelayMultiply);
Console.WriteLine("[Chef] Bacon Fried");
}

private void ToastBread()
{
Console.WriteLine("[Chef] Put bread in toaster");
Thread.Sleep(20 * DelayMultiply);
Console.WriteLine("[Chef] Bread Toasted");
}

private void ApplyButter()
{
Console.WriteLine("[Chef] Start spreading Butter");
Thread.Sleep(15 * DelayMultiply);
Console.WriteLine("[Chef] Butter applied");
}

private void ApplyJam()
{
Console.WriteLine("[Chef] Start spreading Jam");
Thread.Sleep(15 * DelayMultiply);
Console.WriteLine("[Chef] Jam applied");
}

private void PourJuice()
{
Console.WriteLine("[Chef] Start pouring Juice");
Thread.Sleep(5 * DelayMultiply);
Console.WriteLine("[Chef] Juice Ready");
}

private void BoilWater()
{
Console.WriteLine("[Chef] Set coffee water to boil");
Thread.Sleep(200 * DelayMultiply);
Console.WriteLine("[Chef] Coffee Water is boiled");
}
}
11 changes: 11 additions & 0 deletions src/Ch04/csharp-async-example/csharp-async-example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>csharp_async_example</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit e5c7f72

Please sign in to comment.