public async Task DemonstrateAsync() { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); // Start tasks and go ahead Task <Egg> eggsTask = FryEggsAsync(); Task <Bacon> baconTask = FryBaconAsync(); // Start task 'toast' and wait until it finishes Toast toast = await ToastBreadAsync(); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); // Wait for tasks 'eggs' and 'bacon' to be finished Egg eggs = await eggsTask; Console.WriteLine("eggs are ready"); Bacon bacon = await baconTask; Console.WriteLine("bacon is ready"); Console.WriteLine("Breakfast is ready!"); }
private static void MakeBreakfast() { 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!"); }
private static void Main(string[] args) { Stopwatch watch = Stopwatch.StartNew(); Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Task.Run(() => { Egg eggs = FryEggs(2); }); Console.WriteLine("eggs are ready"); Task.Run(() => { Bacon bacon = FryBacon(3); }); Console.WriteLine("bacon is ready"); Toast toast = new Toast(); Task.Run(() => { 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!"); watch.Stop(); double seconds = watch.Elapsed.TotalSeconds; Console.WriteLine(seconds); }
public virtual void ClassMain() { WriteLine("-*-*-*-*-* coffee is prepearing *-*-*-*-*-"); Coffee cup = PourCoffee(); WriteLine("-*-*-*-*-* coffee is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* egg is prepearing *-*-*-*-*-"); Egg eggs = FryEggs(2); WriteLine("-*-*-*-*-* egg is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* bacon is prepearing *-*-*-*-*-"); Bacon bacon = FryBacon(3); WriteLine("-*-*-*-*-* bacon is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* toast is prepearing *-*-*-*-*-"); Toast toast = ToastBread(2); ApplyButter(toast); ApplyJam(toast); WriteLine("-*-*-*-*-* toast is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* Juice is prepearing *-*-*-*-*-"); Juice oj = PourOJ(); // oj = orange juice WriteLine("-*-*-*-*-* Juice is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* Breakfast is ready! *-*-*-*-*-"); }
static async Task CookImprovedBreakfast() { DateTime time = DateTime.Now; Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Task <Egg> eggsTask = FryEggsAsync(2); Task <Bacon> baconTask = FryBaconAsync(3); Task <Toast> toastTask = ToastBreadAsync(2); Toast toast = await toastTask; ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Egg eggs = await eggsTask; Console.WriteLine("eggs are ready"); Bacon bacon = await baconTask; Console.WriteLine("bacon is ready"); Console.WriteLine("Breakfast is ready!"); Console.WriteLine($"It took {DateTime.Now - time} seconds"); }
public async Task FryBaconAsyncIsCalled() { Bacon result = await Program.FryBaconAsync(2); Assert.IsTrue(result.wasCalled); }
public async static Task StartAsyncWorkConcurrentlyAsync(StartAsyncWork concurrently) { concurrently.PourCoffee(); Console.WriteLine("coffee is ready"); Task <Egg> eggsTask = concurrently.FryEggsAsync(2); Egg eggs = await eggsTask; Console.WriteLine("eggs are ready"); Task <Bacon> baconTask = concurrently.FryBaconAsync(3); Bacon bacon = await baconTask; Console.WriteLine("bacon is ready"); Task <Toast> toastTask = concurrently.ToastBreadAsync(2); Toast toast = await toastTask; concurrently.ApplyButter(toast); concurrently.ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = concurrently.PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready!"); }
static void CookBreakFastSyncronized() { DateTime time = DateTime.Now; 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!"); Console.WriteLine($"It took {DateTime.Now - time} seconds"); }
private static void SynchronousPreparation() { Console.WriteLine("Starting synchronous breakfast preparation"); Coffee cup = Coffee.PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = Egg.FryEggs(2); Console.WriteLine("eggs are ready"); Bacon bacon = Bacon.FryBacon(3); Console.WriteLine("bacon is ready"); Toast toast = Toast.ToastBread(2); Toast.ApplyButter(toast); Toast.ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = Juice.PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Synchronous breakfast is ready!"); }
private static async Task NonBlockingPreparation() { Console.WriteLine("Starting non blocking breakfast preparation"); Coffee cup = Coffee.PourCoffee(); Console.WriteLine("coffee is ready"); Egg eggs = await Egg.FryEggsAsync(2); Console.WriteLine("eggs are ready"); Bacon bacon = await Bacon.FryBaconAsync(3); Console.WriteLine("bacon is ready"); Toast toast = await Toast.ToastBreadAsync(2); Toast.ApplyButter(toast); Toast.ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = Juice.PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine("Non-blocking breakfast is ready!"); }
private static async Task ConcurrentPreparation() { Console.WriteLine("Concurrent breakfast preparation"); Coffee cup = Coffee.PourCoffee(); Console.WriteLine("coffee is ready"); Task <Egg> eggsTask = Egg.FryEggsAsync(2); Task <Bacon> baconTask = Bacon.FryBaconAsync(3); Task <Toast> toastTask = Toast.ToastBreadAsync(2); Toast toast = await toastTask; Toast.ApplyButter(toast); Toast.ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = Juice.PourOJ(); Console.WriteLine("oj is ready"); Egg eggs = await eggsTask; Console.WriteLine("eggs are ready"); Bacon bacon = await baconTask; Console.WriteLine("bacon is ready"); Console.WriteLine("Concurrent breakfast is ready!"); }
public void MainMethod() { Task.Delay(2000).Wait(); 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 async void Test() { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Task <Egg> eggTask = FryEggs(2); Task <Bacon> baconTask = FryBacon(3); var toastTask = MakeToastWithButterAndJamAsync(2); //Task<Toast> toastTask = ToastBread(2); //Toast toast = await toastTask; //ApplyButter(toast); //ApplyJam(toast); //Console.WriteLine("toast is ready"); //Juice oj = PourOJ(); //Console.WriteLine("juice is ready"); Egg eggs = await eggTask; Console.WriteLine("eggs are ready"); Bacon bacon = await baconTask; Console.WriteLine("bacon is ready"); var toast = await toastTask; Console.WriteLine("toast is ready"); Juice oj = PourOJ(); Console.WriteLine("juice is ready"); Console.WriteLine("Breakfast is ready!"); }
// Začnimo z običajnim primerom - primer slabe prakse public static void BreakfastBadExample() { Stopwatch sw = Stopwatch.StartNew(); Console.WriteLine("Pripravimo si dober zajtrk brez asinhronih pristopov!"); Coffee cup = PourCoffee(); Console.WriteLine("\n----> coffee is ready"); Egg eggs = FryEggs(2); Console.WriteLine("\n----> eggs are ready"); Bacon bacon = FryBacon(3); Console.WriteLine("\n----> bacon is ready"); Toast toast = ToastBread(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("\n----> toast is ready"); Juice oj = PourOJ(); Console.WriteLine("\n----> oj is ready"); Console.WriteLine("\n----> ----> Breakfast is ready!"); Console.WriteLine($"Zajtrk smo si pripravili v {sw.Elapsed.TotalSeconds} sekundah!"); }
private 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!"); }
public void BreakFastSynchronous() { 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!"); Console.Read(); }
public void MakeBreakFast() { var st = new Stopwatch(); st.Start(); Coffee cup = PourCoffee(1000); Console.WriteLine("coffee is ready"); Egg eggs = FryEggs(2000); Console.WriteLine("eggs are ready"); Bacon bacon = FryBacon(3000); Console.WriteLine("bacon is ready"); Toast toast = ToastBread(2000); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Juice oj = PourOJ(3); Console.WriteLine("oj is ready"); Console.WriteLine("Breakfast is ready! Elapsed: " + st.ElapsedMilliseconds + " ms"); }
static async Task Main(string[] args) { Coffee cup = await PourCoffee(); await Console.Out.WriteLineAsync("Coffee is ready"); Egg eggs = await FryEggs(2); await Console.Out.WriteLineAsync("Eggs are ready"); Bacon bacon = await FryBacon(3); await Console.Out.WriteLineAsync("Bacon is ready"); Toast toast = await ToastBread(2); await ApplyButter(toast); await ApplyJam(toast); await Console.Out.WriteLineAsync("toast is ready"); Juice orange = await PourOJ(); await Console.Out.WriteLineAsync("Orange juice is ready"); await Console.Out.WriteLineAsync("Breakfast is ready!"); }
static async Task Main(string[] args) { Cafe cup = HacerCafe(); Console.WriteLine("cafe listo"); Task <Huevos> eggsTask = FreirHuevos(2); Task <Bacon> baconTask = FreirBacon(3); Task <Tostada> toastTask = TostarPan(2); Tostada toast = await toastTask; await AplicarMantequilla(toast); AplicarJamon(toast); Console.WriteLine("tostadas preparadas"); Zumo oj = HacerZumo(); Console.WriteLine("zumo en su punto"); Huevos eggs = await eggsTask; Console.WriteLine("huevos listos"); Bacon bacon = await baconTask; Console.WriteLine("bacon listo"); Console.WriteLine("desayuno preparado!"); }
static void Main(string[] args) { Console.WriteLine("1. Start!"); Coffee cup = PourCoffee(); Console.WriteLine("3. coffee is ready"); Egg eggs = FryEggs(2); Console.WriteLine("8. eggs are ready"); Bacon bacon = FryBacon(3); Console.WriteLine("14. bacon is ready"); Toast toast = ToastBread(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("20. toast is ready"); Juice oj = PourOJ(); Console.WriteLine("22. oj is ready"); Console.WriteLine("23. Breakfast is ready!"); Console.WriteLine("24. End!"); }
private void button7_Click(object sender, EventArgs e) { Task task = new Task(); task.EscreveLinha += EscreveLinha; EscreveLinha("Inicio:" + DateTime.Now.ToString()); Coffee cup = task.PourCoffee(); EscreveLinha("Café Preto está pronto"); Egg eggs = task.FryEggs(2); EscreveLinha("Ovos estão prontos"); Bacon bacon = task.FryBacon(3); EscreveLinha("Bacon estão prontos"); Toast toast = task.ToastBread(2); task.ApplyButter(toast); task.ApplyJam(toast); EscreveLinha("Torradas estão prontos"); Juice oj = task.PourOJ(); EscreveLinha("Suco de Laranja estão prontos"); EscreveLinha("Cafe da manha concluido!"); EscreveLinha("Fim:" + DateTime.Now.ToString()); }
static async Task Main(string[] args) { Console.WriteLine(DateTime.Now); Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Task <Egg> eggTask = FryEggs(2); Task <Bacon> baconTask = FryBacon(3); Task <Toast> toastTask = MakeToastWithButterAndJamAsync(2); Toast toast = await toastTask; //ApplyButter(toast); //ApplyJam(toast); //Console.WriteLine("toast is ready"); Egg egg = await eggTask; //FryEggs(2); Console.WriteLine("eggs are ready"); Bacon bacon = await baconTask; //FryBacon(3); Console.WriteLine("bacon is ready"); Console.WriteLine("Breakfast is ready!"); Console.WriteLine(DateTime.Now); Console.Read(); }
public async Task MakeBreakfast() { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Task <Egg> eggsTask = FryEggsAsync(2); Task <Bacon> baconTask = FryBaconAsync(3); Task <Toast> toastTask = ToastBreadAsync(2); 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!"); Egg eggs = await eggsTask; Console.WriteLine("eggs are ready"); Bacon bacon = await baconTask; Console.WriteLine("bacon is ready"); }
static void Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); 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(); Console.WriteLine($"Total time : {stopwatch.Elapsed}"); }
/* * This code doesn't block while the eggs or the bacon are cooking. * This code won't start any other tasks though. You'd still put the toast in the toaster and stare at it until it pops. * But at least, you'd respond to anyone that wanted your attention. In a restaurant where multiple orders are placed, * the cook could start another breakfast while the first is cooking. * Now, the thread working on the breakfast isn't blocked while awaiting any started task that hasn't yet finished. * For some applications, this change is all that's needed. * A GUI application still responds to the user with just this change. * However, for this scenario, you want more. You don't want each of the component tasks to be executed sequentially. * It's better to start each of the component tasks before awaiting the previous task's completion. */ public async Task PrepareBreakfastAsync() { Coffee cup = PourCoffee(); Console.WriteLine("coffee is ready"); Console.WriteLine(); Egg eggs = await FryEggs(2); Console.WriteLine("eggs are ready"); Console.WriteLine(); Bacon bacon = await FryBacon(3); Console.WriteLine("bacon is ready"); Console.WriteLine(); Toast toast = await ToastBread(4); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("toast is ready"); Console.WriteLine(); Juice oj = PourOJ(); Console.WriteLine("oj is ready"); Console.WriteLine(); Console.WriteLine("Breakfast is ready!"); }
public async Task ClassMain() { WriteLine("-*-*-*-*-* coffee is prepearing *-*-*-*-*-"); Coffee cup = PourCoffee(); WriteLine("-*-*-*-*-* coffee is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* eggs is prepearing *-*-*-*-*-"); Egg eggs = await FryEggsAsync(2); WriteLine("-*-*-*-*-* eggs is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* bacon is prepearing *-*-*-*-*-"); Bacon bacon = await FryBaconAsync(3); WriteLine("-*-*-*-*-* bacon is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* toast is prepearing *-*-*-*-*-"); Toast toast = await ToastBreadAsync(2); ApplyButter(toast); ApplyJam(toast); WriteLine("-*-*-*-*-* toast is ready *-*-*-*-*-"); WriteLine("-*-*-*-*-* oj is prepearing *-*-*-*-*-"); Juice oj = PourOJ(); // oj = orange juice WriteLine("-*-*-*-*-* oj is ready *-*-*-*-*-"); WriteLine("Breakfast is ready!"); }
static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); Console.OutputEncoding = System.Text.Encoding.UTF8; Coffee cup = PourCoffee(); Console.WriteLine("Кофе готов"); Egg eggs = FryEggs(2); Console.WriteLine("Яица готовы"); Bacon bacon = FryBacon(3); Console.WriteLine("Бекон готов"); Toast toast = ToastBread(2); ApplyButter(toast); ApplyJam(toast); Console.WriteLine("Тосты готовы"); sw.Stop(); Console.WriteLine($"Завтрак готов за {sw.Elapsed.Seconds} минут"); }
static void Main(string[] args) { Console.WriteLine("***** 아침 식사 준비 시작 *****\n"); DateTime startTime = DateTime.Now; 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!"); DateTime endTime = DateTime.Now; TimeSpan elapsed = endTime - startTime; // 두 날짜 사이의 시간 간격 저장 Console.WriteLine($"실행 시간: {elapsed}"); }
public static void Executar() { Cafe cafezinho = ColocarCafe(); Console.WriteLine("cafézinho tá pronto"); Ovo ovos = FritarOvos(2); Console.WriteLine("os ovos estão prontos"); Bacon baconzera = FritarBacon(3); Console.WriteLine("aquele bacon maravilhoso está pronto!"); Pao paozinho = ColocarPaoNaChapa(2); PassarManteiga(paozinho); PassarNutella(paozinho); Console.WriteLine("o pão está pronto"); Suco oj = ColocarSuco(); Console.WriteLine("Suquinho de laranja está ok"); Console.WriteLine("O café da manhã está pronto!!! :D"); }
static void Main(string[] args) { Coffee cup = new Coffee(); cup.pourCoffee(); Egg eggs = new Egg(); eggs.fryEggs(); Bacon bacon = new Bacon(); bacon.fryBacon(); Toast toast = new Toast(); String prepareToast = toast.toastBread(); toast.applyButter(prepareToast); toast.applyJam(prepareToast); Console.WriteLine("toast is ready"); Juice oj = new Juice(); oj.pourJuice(); Console.WriteLine("Breakfast is ready!"); }
public void AddIngredientDoesNotRemoveIngredients() { //Arrange IRecipe recipe = new Bacon(null, DEFAULT_UNIT_SIZE); //Assert bool containsBacon = recipe.printRecipe().ToLower().Contains("bacon"); Assert.IsTrue(containsBacon); //Act recipe = new Salt(recipe, DEFAULT_UNIT_SIZE); //Assert containsBacon = recipe.printRecipe().ToLower().Contains("bacon"); bool containsSalt = recipe.printRecipe().ToLower().Contains("salt"); Assert.IsTrue(containsBacon); Assert.IsTrue(containsSalt); }
public void UntitledTest() { var sandwich = new BaseSandwich(); Assert.AreEqual(sandwich.Price, 1.00m); var wheatBreadSandwich = new WheatBread(sandwich); Assert.AreEqual(wheatBreadSandwich.Price, 2.00m); var hamSandwich = new Ham(wheatBreadSandwich); Assert.AreEqual(hamSandwich.Price, 4.00m); var baconHam = new Bacon(new Bacon(hamSandwich)); Assert.IsTrue(baconHam.InnerComponent is Bacon); Assert.AreEqual(baconHam.Price, 5.00m); var discountedBaconHam = new BaconDiscount(new BaconDiscount(baconHam)); Assert.AreEqual(discountedBaconHam.Price, 4.00m); }