Exemple #1
0
        public static async Task <Egg> FryEggsAsync(int howMany)
        {
            Console.WriteLine("Warming the egg pan...");
            await Task.Delay(3000);

            Console.WriteLine($"cracking {howMany} eggs");
            Console.WriteLine("cooking the eggs ...");
            await Task.Delay(3000);

            Console.WriteLine("Put eggs on plate");

            Egg egg = new Egg();

            egg.wasCalled = true;

            return(egg);
        }
Exemple #2
0
        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();
        }
Exemple #3
0
        // <SnippetMain>
        static async Task Main(string[] args)
        {
            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");
            Egg eggs = await FryEggs(2);

            Console.WriteLine("eggs are ready");
            Bacon bacon = await FryBacon(3);

            Console.WriteLine("bacon is ready");
            Toast toast = await 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 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!");
        }