예제 #1
0
        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);
        }
예제 #2
0
        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!");
        }
예제 #3
0
        // <SnippetMain>
        static async Task Main(string[] args)
        {
            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");

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

            var eggs = await eggsTask;

            Console.WriteLine("eggs are ready");

            var bacon = await baconTask;

            Console.WriteLine("bacon is ready");

            var toast = await toastTask;

            Console.WriteLine("toast is ready");

            Juice oj = PourOJ();

            Console.WriteLine("oj is ready");
            Console.WriteLine("Breakfast is ready!");
        }
예제 #4
0
        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());
        }
예제 #5
0
        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);
        }
예제 #6
0
        // <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!");
        }
예제 #7
0
        public static Juice PourOJ()
        {
            Console.WriteLine("Pouring orange juice");

            Juice juice = new Juice();

            juice.wasCalled = true;

            return(juice);
        }
예제 #8
0
        // <SnippetMain>
        static async Task Main(string[] args)
        {
            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");
            var eggsTask  = FryEggsAsync(2);
            var baconTask = FryBaconAsync(3);
            var toastTask = MakeToastWithButterAndJamAsync(2);

            // <SnippetAwaitAnyTask>
            var allTasks = new List <Task> {
                eggsTask, baconTask, toastTask
            };

            while (allTasks.Any())
            {
                Task finished = await Task.WhenAny(allTasks);

                if (finished == eggsTask)
                {
                    Console.WriteLine("eggs are ready");
                }
                else if (finished == baconTask)
                {
                    Console.WriteLine("bacon is ready");
                }
                else if (finished == toastTask)
                {
                    Console.WriteLine("toast is ready");
                }
                allTasks.Remove(finished);
            }
            Juice oj = PourOJ();

            Console.WriteLine("oj is ready");
            Console.WriteLine("Breakfast is ready!");
            // </SnippetAwaitAnyTask>

            async Task <Toast> MakeToastWithButterAndJamAsync(int number)
            {
                var toast = await ToastBreadAsync(number);

                ApplyButter(toast);
                ApplyJam(toast);
                return(toast);
            }
        }
예제 #9
0
        static async Task CorrectlyAsyncBreakfast(CancellationToken cancellationToken)
        {
            Console.WriteLine("Starting CorrectlyAsync Breakfast");

            Stopwatch sw = new Stopwatch();

            sw.Start();
            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");
            var eggsTask = FryEggsAsync(2);

            TakeOutTheBinAsync();
            var baconTask = FryBaconAsync(3);
            var toastTask = ToastBreadAsync(2);

            Juice oj = PourOJ();

            Console.WriteLine("oj is ready");

            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }
            await eggsTask;

            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }
            await baconTask;

            if (cancellationToken.IsCancellationRequested)
            {
                throw new TaskCanceledException();
            }
            var toast = await toastTask;

            ApplyButter(toast);
            ApplyJam(toast);
            Console.WriteLine("toast is ready");

            sw.Stop();
            Console.WriteLine("CorrectlyAsync Breakfast is ready!");
            Console.WriteLine("It took {0} seconds", sw.ElapsedMilliseconds / 1000M);
        }
예제 #10
0
        static async Task Main(string[] args)
        {
            var sw = Stopwatch.StartNew();

            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");

            Task <Egg>   eggsTask  = FryEggsAsync(2);
            Task <Bacon> baconTask = FryBaconAsync(3);
            Task <Toast> toastTask = MakeToastWithButterAndJamAsync(2);

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

            while (tasks.Count > 0)
            {
                Task finished = await Task.WhenAny(tasks);

                tasks.Remove(finished);

                if (finished == toastTask)
                {
                    Console.WriteLine("toast is ready");
                }
                else if (finished == eggsTask)
                {
                    Console.WriteLine("eggs are ready");
                }
                else if (finished == baconTask)
                {
                    Console.WriteLine("bacon is ready");
                }
            }

            Juice oj = PourOJ();

            Console.WriteLine("oj is ready");

            sw.Stop();

            Console.WriteLine("Breakfast is ready!");
            Console.WriteLine("Total time: " + sw.ElapsedMilliseconds / 1000 + "s");
        }
예제 #11
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();
        }
예제 #12
0
        static async Task Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            Coffee cup = PrepareCoffee();

            Console.WriteLine($" qahva tayyor");

            var eggsTask       = FryEggsAsync(2);
            var toastTask      = MakeToastWithJamAsync(2);
            var breakfastTasks = new List <Task> {
                eggsTask, toastTask
            };

            while (breakfastTasks.Count > 0)
            {
                Task finishedTask = await Task.WhenAny(breakfastTasks);

                if (finishedTask == eggsTask)
                {
                    Console.WriteLine($" tuxum tayyor");
                }
                else if (finishedTask == toastTask)
                {
                    Console.WriteLine($" non tayyor");
                }
                breakfastTasks.Remove(finishedTask);
            }

            Juice juice = PourJuice();

            Console.WriteLine($" sharbat tayyor");
            Console.WriteLine($" Nonushta tayyor!");
            sw.Stop();
            Console.WriteLine($" Nonushtaga ketgan vaqt {sw.ElapsedMilliseconds / 1000} soniya");
            Console.Read();
        }
예제 #13
0
        static async Task Main(string[] args)
        {
            Coffee cup = PourCoffee();

            Console.WriteLine("Coffee is ready");

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

            var eggs = await eggsTask;

            Console.WriteLine("Eggs are ready");

            var bacon = await baconTask;

            Console.WriteLine("Bacon is ready");

            var toast = await toastTask;

            Console.WriteLine("Toast is ready");

            Juice oj = PourOJ();

            Console.WriteLine("OJ is ready");

            Console.WriteLine("Breakfast is ready!");

            async Task <Toast> makeToastWithButterAndJamAsync(int number)
            {
                var plainToast = await ToastBreadAsync(number);

                ApplyButter(plainToast);
                ApplyJam(plainToast);

                return(plainToast);
            }
        }
예제 #14
0
        public static async void PrepareBreakfast()
        {
            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");

            var eggsTask  = FryEggsAsync(2);
            var baconTask = FryBaconAsync(3);
            var toastTask = 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 = PourOJ();

            Console.WriteLine("oj is ready");
            Console.WriteLine("Breakfast is ready!");
        }
예제 #15
0
        // <SnippetMain>
        static async Task Main(string[] args)
        {
            Coffee cup = PourCoffee();

            Console.WriteLine("coffee is ready");
            var eggsTask  = FryEggsAsync(2);
            var baconTask = FryBaconAsync(3);
            var toastTask = MakeToastWithButterAndJamAsync(2);

            var eggs = await eggsTask;

            Console.WriteLine("eggs are ready");
            var bacon = await baconTask;

            Console.WriteLine("bacon is ready");
            var toast = await toastTask;

            Console.WriteLine("toast is ready");
            Juice oj = PourOJ();

            Console.WriteLine("oj is ready");

            Console.WriteLine("Breakfast is ready!");

            // <SnippetComposeToastTask>
            async Task <Toast> MakeToastWithButterAndJamAsync(int number)
            {
                var toast = await ToastBreadAsync(number);

                ApplyButter(toast);
                ApplyJam(toast);
                return(toast);
            }

            // </SnippetComposeToastTask>
        }
예제 #16
0
        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!");
        }