{"id":248,"date":"2021-04-01T19:46:31","date_gmt":"2021-04-01T10:46:31","guid":{"rendered":"https:\/\/mvc.auctionpro.co.kr\/?page_id=248"},"modified":"2024-03-12T10:55:19","modified_gmt":"2024-03-12T01:55:19","slug":"asynchronous-programming-with-async-and-await","status":"publish","type":"page","link":"https:\/\/mvc.auctionpro.co.kr\/?page_id=248","title":{"rendered":"Asynchronous programming with async and await"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Remark :<\/h3>\n\n\n\n<p>&#8220;\ube44\ub3d9\uae30(Asynchronous)&#8221;\uc758 \ub77c\ud2f4\uc5b4 \uc5b4\uc6d0\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>&#8220;Asyn&#8221;\uc740 \ub77c\ud2f4\uc5b4\uc5d0\uc11c &#8220;non&#8221; \ub610\ub294 &#8220;not&#8221;\uc744 \uc758\ubbf8\ud558\ub294 &#8220;a-&#8221; \uc811\ub450\uc0ac\uc640 &#8220;together&#8221;\ub97c \uc758\ubbf8\ud558\ub294 &#8220;syn-&#8220;\uc758 \uacb0\ud569\uc785\ub2c8\ub2e4.<\/li>\n\n\n\n<li>&#8220;Chronous&#8221;\ub294 \ub77c\ud2f4\uc5b4 &#8220;chronus&#8221;\uc5d0\uc11c \ud30c\uc0dd\ub418\uc5c8\uc73c\uba70, &#8220;\uc2dc\uac04&#8221;\uc744 \uc758\ubbf8\ud569\ub2c8\ub2e4.<\/li>\n<\/ol>\n\n\n\n<p>\ub530\ub77c\uc11c &#8220;Asynchronous&#8221;\ub294 &#8220;\uc2dc\uac04\uc801\uc73c\ub85c \ub3d9\uc77c\ud558\uc9c0 \uc54a\ub294&#8221; \ub610\ub294 &#8220;\ub3d9\uc2dc\uc5d0 \uc77c\uc5b4\ub098\uc9c0 \uc54a\ub294&#8221;\uc774\ub77c\ub294 \uc758\ubbf8\ub97c \uac16\uc2b5\ub2c8\ub2e4.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Microsoft Visual Studio Community 2019<br>\ubc84\uc804 16.9.3<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">The Task asynchronous programming model (TAP) provides an abstraction over asynchronous code<\/h4>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"318\" src=\"https:\/\/mvc.auctionpro.co.kr\/wp-content\/uploads\/2021\/04\/whenany-async-breakfast.png\" alt=\"\" class=\"wp-image-245\" srcset=\"https:\/\/mvc.auctionpro.co.kr\/wp-content\/uploads\/2021\/04\/whenany-async-breakfast.png 774w, https:\/\/mvc.auctionpro.co.kr\/wp-content\/uploads\/2021\/04\/whenany-async-breakfast-300x123.png 300w, https:\/\/mvc.auctionpro.co.kr\/wp-content\/uploads\/2021\/04\/whenany-async-breakfast-768x316.png 768w\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">using Newtonsoft.Json;\nusing System;\nusing System.IO;\nusing System.Net.Http;\nusing System.Net.Http.Json;\nusing System.Threading.Tasks;\nusing System.Collections.Generic;\n\nnamespace SystemNetHttpJsonSamples\n{\n    partial class Program\n    {\n        static async Task Main(string[] args)\n        {\n            Coffee cup = PourCoffee();\n            Console.WriteLine(\"coffee is ready\");\n\n            var eggsTask = FryEggsAsync(2);\n            var baconTask = FryBaconAsync(3);\n            var toastTask = MakeToastWithButterAndJamAsync(2);\n\n            var breakfastTasks = new List&lt;Task&gt; { eggsTask, baconTask, toastTask };\n            while (breakfastTasks.Count &gt; 0)\n            {\n                Task finishedTask = await Task.WhenAny(breakfastTasks);\n                if (finishedTask == eggsTask)\n                {\n                    Console.WriteLine(\"eggs are ready\");\n                }\n                else if (finishedTask == baconTask)\n                {\n                    Console.WriteLine(\"bacon is ready\");\n                }\n                else if (finishedTask == toastTask)\n                {\n                    Console.WriteLine(\"toast is ready\");\n                }\n                breakfastTasks.Remove(finishedTask);\n            }\n\n            Juice oj = PourOJ();\n            Console.WriteLine(\"oj is ready\");\n            Console.WriteLine(\"Breakfast is ready!\");\n        }\n\n        static async Task&lt;Toast&gt; MakeToastWithButterAndJamAsync(int number)\n        {\n            var toast = await ToastBreadAsync(number);\n            ApplyButter(toast);\n            ApplyJam(toast);\n\n            return toast;\n        }\n\n        private static Juice PourOJ()\n        {\n            Console.WriteLine(\"Pouring orange juice\");\n            return new Juice();\n        }\n\n        private static void ApplyJam(Toast toast) =&gt;\n            Console.WriteLine(\"Putting jam on the toast\");\n\n        private static void ApplyButter(Toast toast) =&gt;\n            Console.WriteLine(\"Putting butter on the toast\");\n\n        private static async Task&lt;Toast&gt; ToastBreadAsync(int slices)\n        {\n            for (int slice = 0; slice &lt; slices; slice++)\n            {\n                Console.WriteLine(\"Putting a slice of bread in the toaster\");\n            }\n            Console.WriteLine(\"Start toasting...\");\n            await Task.Delay(3000);\n            Console.WriteLine(\"Remove toast from toaster\");\n\n            return new Toast();\n        }\n\n        private static async Task&lt;Bacon&gt; FryBaconAsync(int slices)\n        {\n            Console.WriteLine($\"putting {slices} slices of bacon in the pan\");\n            Console.WriteLine(\"cooking first side of bacon...\");\n            await Task.Delay(3000);\n            for (int slice = 0; slice &lt; slices; slice++)\n            {\n                Console.WriteLine(\"flipping a slice of bacon\");\n            }\n            Console.WriteLine(\"cooking the second side of bacon...\");\n            await Task.Delay(3000);\n            Console.WriteLine(\"Put bacon on plate\");\n\n            return new Bacon();\n        }\n\n        private static async Task&lt;Egg&gt; FryEggsAsync(int howMany)\n        {\n            Console.WriteLine(\"Warming the egg pan...\");\n            await Task.Delay(3000);\n            Console.WriteLine($\"cracking {howMany} eggs\");\n            Console.WriteLine(\"cooking the eggs ...\");\n            await Task.Delay(3000);\n            Console.WriteLine(\"Put eggs on plate\");\n\n            return new Egg();\n        }\n\n        private static Coffee PourCoffee()\n        {\n            Console.WriteLine(\"Pouring coffee\");\n            return new Coffee();\n        }\n\n\n\n        private class Coffee\n        {\n        }\n\n        private class Egg\n        {\n        }\n\n        private class Bacon\n        {\n            public Bacon()\n            {\n            }\n        }\n\n        private class Toast\n        {\n            public Toast()\n            {\n            }\n        }\n\n        private class Juice\n        {\n            public Juice()\n            {\n            }\n        }\n    }\n}\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">Pouring coffee\ncoffee is ready\nWarming the egg pan...\nputting 3 slices of bacon in the pan\ncooking first side of bacon...\nPutting a slice of bread in the toaster\nPutting a slice of bread in the toaster\nStart toasting...\nflipping a slice of bacon\nflipping a slice of bacon\nflipping a slice of bacon\ncooking the second side of bacon...\ncracking 2 eggs\ncooking the eggs ...\nRemove toast from toaster\nPutting butter on the toast\nPutting jam on the toast\ntoast is ready\nPut bacon on plate\nPut eggs on plate\nbacon is ready\neggs are ready\nPouring orange juice\noj is ready\nBreakfast is ready!<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remark : &#8220;\ube44\ub3d9\uae30(Asynchronous)&#8221;\uc758 \ub77c\ud2f4\uc5b4 \uc5b4\uc6d0\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4: \ub530\ub77c\uc11c &#8220;Asynchronous&#8221;\ub294 &#8220;\uc2dc\uac04\uc801\uc73c\ub85c \ub3d9\uc77c\ud558\uc9c0 \uc54a\ub294&#8221; \ub610\ub294 &#8220;\ub3d9\uc2dc\uc5d0 \uc77c\uc5b4\ub098\uc9c0 \uc54a\ub294&#8221;\uc774\ub77c\ub294 \uc758\ubbf8\ub97c \uac16\uc2b5\ub2c8\ub2e4. Microsoft Visual Studio Community 2019\ubc84\uc804 16.9.3 The Task asynchronous programming model (TAP) provides an abstraction over asynchronous code using Newtonsoft.Json; using System; using System.IO; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; using System.Collections.Generic; namespace SystemNetHttpJsonSamples { partial class\u2026 <span class=\"read-more\"><a href=\"https:\/\/mvc.auctionpro.co.kr\/?page_id=248\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-248","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":2,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/248\/revisions"}],"predecessor-version":[{"id":806,"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/pages\/248\/revisions\/806"}],"wp:attachment":[{"href":"https:\/\/mvc.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}