using System; using System.Threading.Tasks; namespaceAsyncBreakfast { // These classes are intentionally empty for the purpose of this example. They are simply marker classes for the purpose of demonstration, contain no properties, and serve no other purpose. internalclassHashBrown { } internalclassCoffee { } internalclassEgg { } internalclassJuice { } internalclassToast { } classProgram { staticvoidMain(string[] args) { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = FryEggs(2); Console.WriteLine("eggs are ready"); HashBrown hashBrown = FryHashBrowns(3); Console.WriteLine("hash browns are ready"); Toast toast = ToastBread(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready!"); }
privatestatic Juice PourOJ() { Console.WriteLine("Pouring orange juice"); returnnew Juice(); }
privatestaticvoidApplyJam(Toast toast) => Console.WriteLine("Putting jam on the toast");
privatestaticvoidApplyButter(Toast toast) => Console.WriteLine("Putting butter on the toast");
privatestatic Toast ToastBread(int slices) { for (int slice = 0; slice < slices; slice++) { Console.WriteLine("Putting a slice of bread in the toaster"); } Console.WriteLine("Start toasting..."); Task.Delay(3000).Wait(); Console.WriteLine("Remove toast from toaster"); returnnew Toast(); }
privatestatic HashBrown FryHashBrowns(int patties) { Console.WriteLine($"putting {patties} hash brown patties in the pan"); Console.WriteLine("cooking first side of hash browns..."); Task.Delay(3000).Wait(); for (int patty = 0; patty < patties; patty++) { Console.WriteLine("flipping a hash brown patty"); } onsole.WriteLine("cooking the second side of hash browns..."); Task.Delay(3000).Wait(); Console.WriteLine("Put hash browns on plate"); returnnew HashBrown(); }
privatestatic Egg FryEggs(int howMany) { Console.WriteLine("Warming the egg pan..."); Task.Delay(3000).Wait(); Console.WriteLine($"cracking {howMany} eggs"); Console.WriteLine("cooking the eggs ..."); Task.Delay(3000).Wait(); Console.WriteLine("Put eggs on plate"); returnnew Egg(); }
staticasync Task Main(string[] args) { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); var eggsTask = FryEggsAsync(2); var hashBrownTask = FryHashBrownsAsync(3); var toastTask = MakeToastWithButterAndJamAsync(2); var eggs = await eggsTask; Console.WriteLine("eggs are ready"); var hashBrown = await hashBrownTask; Console.WriteLine("hash browns are ready"); var toast = await toastTask; Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready!"); }
Pouring coffee Coffee is ready Warming the egg pan... putting 3 hash brown patties in the pan Cooking first side of hash browns... Putting a slice of bread in the toaster Putting a slice of bread in the toaster Start toasting... Fire! Toast is ruined! Flipping a hash brown patty Flipping a hash brown patty Flipping a hash brown patty Cooking the second side of hash browns... Cracking 2 eggs Cooking the eggs ... Put hash browns on plate Put eggs on plate Eggs are ready Hash browns are ready Unhandled exception. System.InvalidOperationException: The toaster is on fire at AsyncBreakfast.Program.ToastBreadAsync(Int32 slices) in Program.cs:line 65 at AsyncBreakfast.Program.MakeToastWithButterAndJamAsync(Int32 number) in Program.cs:line 36 at AsyncBreakfast.Program.Main(String[] args) in Program.cs:line 24 at AsyncBreakfast.Program.<Main>(String[] ar
await Task.WhenAll(eggsTask, hashBrownTask, toastTask); Console.WriteLine("Eggs are ready"); Console.WriteLine("Hash browns are ready"); Console.WriteLine("Toast is ready"); Console.WriteLine("Breakfast is ready!");