Beispiel #1
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!");
        }
Beispiel #2
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();
        }
Beispiel #3
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 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!");
        }
        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);
            }
        }
Beispiel #5
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>
        }
        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!");
        }
Beispiel #7
0
        static async Task Main(string[] args)
        {
            // solution(15958);
            var watch = new Stopwatch();

            watch.Start();
            //
            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("\neggs are ready");
                    Console.WriteLine($" ElapsedMilliseconds : {watch.ElapsedMilliseconds}ms");
                }
                else if (finishedTask == baconTask)
                {
                    Console.WriteLine("\nbacon is ready");
                    Console.WriteLine($" ElapsedMilliseconds : {watch.ElapsedMilliseconds}ms");
                }
                else if (finishedTask == toastTask)
                {
                    Console.WriteLine("\ntoast is ready");
                    Console.WriteLine($" ElapsedMilliseconds : {watch.ElapsedMilliseconds}ms");
                }
                breakfastTasks.Remove(finishedTask);
            }
            Console.WriteLine($" ElapsedMilliseconds : {watch.ElapsedMilliseconds}ms");

            Console.WriteLine($" .NET count : {await GetDotNetCount()}");

            Console.WriteLine($" ElapsedMilliseconds : {watch.ElapsedMilliseconds}ms");

            //Coffee cup = PourCoffee();
            //Console.WriteLine("coffee is ready");
            //Console.WriteLine("1");
            //Egg eggs = await FryEggsAsync(2);
            //Console.WriteLine("eggs are ready");
            //Console.WriteLine("2");
            //Bacon bacon = await FryBaconAsync(3);
            //Console.WriteLine("bacon is ready");
            //Console.WriteLine("3");
            //Toast toast = await ToastBreadAsync(2);
            //ApplyButter(toast);
            //ApplyJam(toast);
            //Console.WriteLine("toast is ready");
            //Console.WriteLine("4");
            //Juice oj = PourOJ();
            //Console.WriteLine("oj is ready");
            //Console.WriteLine("Breakfast is ready!");
            //Console.WriteLine("5");
        }