static async Task NotReallyAsyncBreakfast() { Console.WriteLine("Starting NotReallyAsync Breakfast"); Stopwatch sw = new Stopwatch(); sw.Start(); Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = await FryEggsAsync(2); TakeOutTheBinAsync(); Bacon bacon = await FryBaconAsync(3); Toast toast = await ToastBreadAsync(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); sw.Stop(); Console.WriteLine("NotReallyAsync Breakfast is ready!"); Console.WriteLine("It took {0} seconds", sw.ElapsedMilliseconds / 1000M); }
static void Main(string[] args) { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = FryEggs(2); Console.WriteLine("eggs are ready"); Bacon bacon = FryBacon(3); Console.WriteLine("bacon is 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!"); }
static void MakeSyncronousBreakfast() { Console.WriteLine("Starting Sync Breakfast"); Stopwatch sw = new Stopwatch(); sw.Start(); Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = FryEggs(2); TakeOutTheBinAsync(); Console.WriteLine("eggs are ready"); Bacon bacon = FryBacon(3); Console.WriteLine("bacon is ready"); Toast toast = ToastBread(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); sw.Stop(); Console.WriteLine("Sync Breakfast is ready!"); Console.WriteLine("It took {0} seconds", sw.ElapsedMilliseconds / 1000M); }
// <SnippetMain> static async Task Main(string[] args) { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = await FryEggsAsync(2); Console.WriteLine("eggs are ready"); Bacon bacon = await FryBaconAsync(3); Console.WriteLine("bacon is ready"); Toast toast = await ToastBreadAsync(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready!"); }
static void Main(string[] args) { var stopWatch = System.Diagnostics.Stopwatch.StartNew(); Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = FryEggs(2); Console.WriteLine("eggs are ready"); Bacon bacon = FryBacon(3); Console.WriteLine("bacon is 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!"); stopWatch.Stop(); var elapsedTime = stopWatch.Elapsed.Seconds; Console.WriteLine($"Time took to make breakfast: {elapsedTime}".ToUpper()); }
public static async Task <Bacon> FryBaconAsync(int slices) { Console.WriteLine($"putting {slices} slices of bacon in the pan"); Console.WriteLine("cooking first side of bacon..."); await Task.Delay(3000); for (int slice = 0; slice < slices; slice++) { Console.WriteLine("flipping a slice of bacon"); } Console.WriteLine("cooking the second side of bacon..."); await Task.Delay(3000); Console.WriteLine("Put bacon on plate"); Bacon bacon = new Bacon(); bacon.wasCalled = true; return(bacon); }
static async Task Main(string[] args) { Coffee cup = Coffee.PourCoffee(); Console.WriteLine("coffee is ready"); var eggsTask = Egg.FryEggsAsync(2); var baconTask = Bacon.FryBaconAsync(3); var toastTask = Toast.MakeToastWithButterAndJamAsync(2); var breakfastTasks = new List <Task> { eggsTask, baconTask, toastTask }; while (breakfastTasks.Count > 0) { Task finishedTask = await Task.WhenAny(breakfastTasks); if (finishedTask == eggsTask) { Console.WriteLine("eggs are ready"); } else if (finishedTask == baconTask) { Console.WriteLine("bacon is ready"); } else if (finishedTask == toastTask) { Console.WriteLine("toast is ready"); } breakfastTasks.Remove(finishedTask); } Juice oj = Juice.PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready!"); Console.ReadKey(); }
static async Task Main2(string[] args) { Console.WriteLine("SYNCHRONOUS METHOD CALLS"); Coffee cup = new Coffee(); Egg eggs = new Egg(); Bacon bacon = new Bacon(); Toast toast = new Toast(); Juice oj = new Juice(); //cup = PourCoffee(); //Console.WriteLine("coffee is ready"); //eggs = FryEggs(2); //Console.WriteLine("eggs are ready"); //bacon = FryBacon(3); //Console.WriteLine("bacon is ready"); //toast = ToastBread(2); //ApplyButter(toast); //ApplyJam(toast); //Console.WriteLine("toast is ready"); //oj = PourOJ(); //Console.WriteLine("oj is ready"); //Console.WriteLine("Breakfast is ready!"); Console.WriteLine("------------------------------"); Console.WriteLine("A-SYNCHRONOUS METHOD CALLS"); cup = PourCoffee(); Console.WriteLine("coffee is ready"); Task <Egg> eggsTask = FryEggsAsync(2); Task <Bacon> baconTask = FryBaconAsync(3); Task <Toast> toastTask = MakeToastWithButterAndJamAsync(2); // composition of async methods // changed to MakeToastWithButterAndJamAsync => composition of async functions //toast = await toastTask; //ApplyButter(toast); //ApplyJam(toast); oj = PourOJ(); Console.WriteLine("oj is ready"); //best way => putting all the tasks in the list and checking which one done //by using Task.WhenAny methods and removing from the list var breakfastTasks = new List <Task> { eggsTask, baconTask, toastTask }; while (breakfastTasks.Count() > 0) { var completedTask = await Task.WhenAny(breakfastTasks); if (completedTask == eggsTask) { Console.WriteLine("eggs are ready"); } if (completedTask == baconTask) { Console.WriteLine("bacon is ready"); } if (completedTask == toastTask) { Console.WriteLine("toast is ready"); } breakfastTasks.Remove(completedTask); } //instead of listing all the await task statements //await Task.WhenAll(eggsTask, baconTask, toastTask); //changed to WhenAll method //eggs = await eggsTask; //Console.WriteLine("eggs are ready"); //bacon = await baconTask; //Console.WriteLine("bacon is ready"); //toast = await toastTask; //Console.WriteLine("toast is ready"); Console.WriteLine("Breakfast is ready!"); }