{"id":543,"date":"2019-09-25T21:14:12","date_gmt":"2019-09-25T12:14:12","guid":{"rendered":"http:\/\/shinke1987.net\/?p=543"},"modified":"2019-09-25T21:30:38","modified_gmt":"2019-09-25T12:30:38","slug":"post-543","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=543","title":{"rendered":"C#\u3067async\/await\u306e\u30c6\u30b9\u30c8\u3002"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u74b0\u5883<\/h2>\n\n\n\n<p>VisualStudio Pro 2017<br>.NET Framework 4.7.2<br>\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3<\/p>\n\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">\u53c2\u8003\u306b\u3057\u305f\u30b5\u30a4\u30c8<\/h2>\n\n\n\n<p><a href=\"https:\/\/qiita.com\/rawr\/items\/5d49960a4e4d3823722f\" target=\"_blank\" rel=\"noopener noreferrer\">C# \u4eca\u66f4\u3067\u3059\u304c\u3001await \/ async<\/a><\/p>\n\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<p><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Net;\nusing System.Threading;\n\nnamespace _20190925_AsyncTest\n{\n    class Program\n    {\n        static void Main(string&#x5B;] args)\n        {\n            Program program = new Program();\n\n            int&#x5B;] intTemp = new int&#x5B;5];\n\n            \/\/ ===== \u3053\u3053\u304b\u3089 \u901a\u5e38\u306e\u540c\u671f\u51e6\u7406 =====\n            for (int i = 0; i &lt; 5; i++)\n            {\n                intTemp&#x5B;i] = program.Calculate();\n                Console.WriteLine(&quot;\u540c\u671f\u51e6\u7406 : {0}&quot;, i);\n            }\n            for (int i = 0; i &lt; 5; i++)\n            {\n                Console.WriteLine(&quot;{0}\u304cCalculate()\u306e\u7d50\u679c\u3067\u3059\u3002&quot;, intTemp&#x5B;i]);\n            }\n\n            Console.WriteLine(&quot;\u30a8\u30f3\u30bf\u30fc\u30ad\u30fc\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002&quot;);\n            Console.ReadLine();\n            \/\/ ===== \u3053\u3053\u307e\u3067 \u901a\u5e38\u306e\u540c\u671f\u51e6\u7406 =====\n\n            \/\/ ===== \u3053\u3053\u304b\u3089 \u975e\u540c\u671f\u51e6\u7406\uff08\u3053\u306e\u5834\u5408\u4e26\u5217\u51e6\u7406\u304b\u3082\uff09 =====\n            Task&lt;int&gt;&#x5B;] tasks = new Task&lt;int&gt;&#x5B;5];\n\n            for (int i = 0; i &lt; 5; i++)\n            {\n                tasks&#x5B;i] = program.CalculateAsync();\n                Console.WriteLine(&quot;\u975e\u540c\u671f\u51e6\u7406 : {0}&quot;, i);\n                \/\/ \u3053\u3053\u306btasks&#x5B;i].Result\u3068\u66f8\u304b\u308c\u305f\u6587\u304c\u3042\u308b\u3068\u540c\u671f\u51e6\u7406\u3068\u540c\u3058\u306b\u306a\u308b\u3002\n                \n            }\n            for (int i = 0; i &lt; 5; i++)\n            {\n                Console.WriteLine(&quot;{0}\u304cCalculateAsync()\u306e\u7d50\u679c\u3067\u3059\u3002&quot;, tasks&#x5B;i].Result);\n            }\n\n            Console.WriteLine(&quot;\u30a8\u30f3\u30bf\u30fc\u30ad\u30fc\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002&quot;);\n            Console.ReadLine();\n            \/\/ ===== \u3053\u3053\u307e\u3067 \u975e\u540c\u671f\u51e6\u7406\uff08\u3053\u306e\u5834\u5408\u4e26\u5217\u51e6\u7406\u304b\u3082\uff09 =====\n        }\n\n        int Calculate()\n        {\n            \/\/ \u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f4\u79d2\u304b\u304b\u308b\u51e6\u7406\u3002\n            int total = 0;\n            for (int i = 0; i &lt;= 100; i++)\n            {\n                total += i;\n                Thread.Sleep(40);\n            }\n\n            return total;\n        }\n\n        private async Task&lt;int&gt; CalculateAsync()\n        {\n            \/\/ Calculate()\u30e1\u30bd\u30c3\u30c9\u3092\u975e\u540c\u671f\u306b\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3002\n            var task = Task.Run(new Func&lt;int&gt;(this.Calculate));\n            var result = await task;\n            return result;\n        }\n\n    }\n}\n<\/pre>\n<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u74b0\u5883 VisualStudio Pro 2017.NET Framework 4.7.2\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3 \u53c2\u8003\u306b\u3057\u305f\u30b5\u30a4\u30c8 C# \u4eca\u66f4\u3067\u3059\u304c\u3001await \/ async \u30b3\u30fc\u30c9 using System; us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[36],"class_list":["post-543","post","type-post","status-publish","format-standard","hentry","category-c-sharp","tag-c-sharp"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=543"}],"version-history":[{"count":25,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions"}],"predecessor-version":[{"id":579,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions\/579"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}