예제 #1
0
        private static Juice PourOJ()
        {
            Juice juice = new Juice();

            juice.Name = "Orange Juice";
            return(juice);
        }
예제 #2
0
        /*
         * Makes a breakfast asynchronously.
         */
        /// <summary>
        ///  Makes a breakfast asynchronously.
        /// </summary>
        /// <returns>
        ///  A Task representing whether MakeBreakfast is complete or not.
        /// </returns>
        public static async Task MakeBreakfast()
        {
            Coffee cup = PourCoffee();

            Console.WriteLine($"Coffee has been poured and is {(cup.IsFresh ? "fresh" : "not fresh")}");

            var eggsTask  = FryEggsAsync(2);
            var baconTask = FryBaconAsync(3);
            var toastTask = MakeToastWithButterAndJamAsync(2);

            var breakfastTasks = new List <Task> {
                eggsTask, baconTask, toastTask
            };

            while (breakfastTasks.Count > 0)
            {
                // When any task from breakfastTags is finished, the finished task is returned
                Task finishedTask = await Task.WhenAny(breakfastTasks);

                if (finishedTask == eggsTask)
                {
                    Console.WriteLine("Eggs are finished");
                }
                else if (finishedTask == baconTask)
                {
                    Console.WriteLine("Bacon is finished");
                }
                else if (finishedTask == toastTask)
                {
                    Console.WriteLine("Toast has toasted");
                }

                breakfastTasks.Remove(finishedTask);
            }

            Juice oj = PourOJ();

            Console.WriteLine($"{oj.Name} is ready");
            Console.WriteLine("Breakfast is ready");
        }