{"id":2176,"date":"2021-09-25T05:08:36","date_gmt":"2021-09-24T21:08:36","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2176"},"modified":"2022-02-16T17:51:16","modified_gmt":"2022-02-16T09:51:16","slug":"async-await","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/09\/25\/async-await\/","title":{"rendered":"D-20 \u975e\u540c\u6b65 ? async ? await"},"content":{"rendered":"<h1>\u5982\u4f55\u540c\u6642\u9032\u884c<\/h1>\n<p>\u6628\u5929\u7684\u65e5\u5b50\u4e2d\u5c0f\u5149\u5b78\u5230\u4e86\u5982\u4f55\u4f7f\u7528<code>delegate<\/code>\u8ddf<code>Linq<\/code>\uff0c\u9019\u8b93\u5c0f\u5149\u5728\u8cc7\u6599\u8655\u7406\u4e2d\u7684\u7a0b\u5f0f\u958b\u767c\u5927\u5927\u9032\u6b65\uff0c\u4e0d\u904e\u5728\u8cc7\u6599\u91cf\u5927\u6642\u57f7\u884c\u901f\u5ea6\u5c31\u986f\u5f97\u7de9\u6162\uff0c\u6240\u4ee5\u5c0f\u5149\u5c31\u628a\u9019\u554f\u984c\u62ff\u4f86\u8acb\u6559\u5927\u982d\uff0c\u770b\u770b\u5927\u982d\u6709\u4ec0\u9ebc\u89e3\u6cd5\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u975e\u540c\u6b65\u7a0b\u5f0f\u958b\u767c<\/h2>\n<p>\u300c\u524d\u8f29\uff0c\u6211\u6628\u5929\u5b78\u5230<code>delegate<\/code>\u8ddf<code>Linq<\/code>\u4e4b\u5f8c\u7acb\u523b\u56de\u5bb6\u8a66\u9a57\uff0c\u679c\u771f\u53ef\u4ee5\u628a\u8a9e\u6cd5\u5beb\u5f97\u597d\u7c21\u6f54\u5537\uff0c\u4e0d\u904e\u6709\u9047\u5230\u57f7\u884c\u901f\u5ea6\u5f88\u6162\u7684\u554f\u984c\u8036\u300d<br \/>\n\u807d\u5230\u9019\u500b\uff0c\u5927\u982d\u8ddf\u4ee5\u5f80\u7684\u57cb\u982d\u82e6\u5e79\u4e0d\u4e00\u6a23\uff0c\u9019\u6642\u7acb\u523b\u62ac\u8d77\u982d\u4f86\u4e0d\u89e3\u5730\u770b\u8457\u5c0f\u5149\u9019\u6a23\u5b50\u554f\u3002<br \/>\n\u300c\u4f60\u7684\u8cc7\u6599\u5230\u5e95\u662f\u591a\u5927\u554a\uff0c\u5927\u5230\u57f7\u884c\u901f\u5ea6\u5f88\u6162\u3002\u7167\u7406\u4f86\u8aaa\u5e7e\u5343\u5e7e\u842c\u7b46\u8cc7\u6599\u7684<code>Linq<\/code>\u5728\u73fe\u5728\u96fb\u8166\u4f86\u8aaa\u61c9\u8a72\u4e0d\u6703\u6162\u624d\u5c0d\uff0c\u4e0d\u904e\u8d85\u904e\u7684\u8a71\u5c31\u4e0d\u6562\u80af\u5b9a\u4e86\u3002\u300d<br \/>\n\u807d\u5b8c\u5927\u982d\u9019\u9ebc\u8aaa\uff0c\u5c0f\u5149\u9677\u5165\u4e86\u6c88\u601d\u4e4b\u4e2d\uff0c\u52aa\u529b\u5730\u601d\u7d22\u81ea\u5df1\u5230\u5e95\u5beb\u4e86\u4ec0\u9ebc\uff0c\u904e\u4e86\u4e00\u9663\u5b50\u5f8c\u5c0f\u5149\u624d\u9019\u9ebc\u8aaa\u3002<br \/>\n\u300c\u6703\u4e0d\u6703\u6211\u5beb\u4e86\u4e00\u5806\u5224\u65b7\u5f0f\u8ddf\u4e00\u5806\u7a0b\u5f0f\u908f\u8f2f\u5728<code>Linq<\/code>\u5167\u3002\u300d<br \/>\n\u807d\u5b8c\u5c0f\u5149\u7684\u8aaa\u660e\u5f8c\u5927\u982d\u9ede\u9ede\u982d\uff0c\u4e26\u4e14\u9019\u6a23\u8aaa\u3002<br \/>\n\u300c\u6709\u53ef\u80fd\u5594\uff0c\u56e0\u70ba\u7c21\u6f54\u6240\u4ee5\u901a\u5e38\u6703\u6709\u4eba\u5beb\u7684\u5305\u5c71\u5305\u6d77\uff0c\u9019\u6a23\u6703\u9020\u6210\u57f7\u884c\u901f\u5ea6\u4e0d\u5f70\uff0c\u9664\u6b64\u4e4b\u5916\u4e5f\u8981\u614e\u9078\u8cc7\u6599\u7d50\u69cb\u5594\u5982\u679c\u6709\u552f\u4e00<code>Key<\/code>\u6642\u9700\u8981\u7528<code>Dictionary<\/code>\u9019\u6a23\u53ef\u4ee5\u6539\u5584\u6548\u80fd\u4e0d\u5f70\u7684\u72c0\u6cc1\u3002\u300d<br \/>\n\u8b1b\u5b8c\u4e4b\u5f8c\u63db\u5927\u982d\u9677\u5165\u6c88\u601d\u4e4b\u4e2d\uff0c\u5f85\u904e\u4e86\u5e7e\u5206\u9418\u5f8c\u5927\u982d\u9019\u6a23\u5b50\u8aaa\u3002<br \/>\n\u300c\u4e0d\u904e\u5982\u679c\u771f\u7684\u9700\u8981\u8655\u7406\u7684\u6771\u897f\u592a\u591a\u6642\uff0c\u90a3\u5c31\u8981\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/csharp\/programming-guide\/concepts\/async\/\" title=\"\u4f7f\u7528 async \u548c await \u9032\u884c\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08\" rel=\"nofollow noopener\" target=\"_blank\">\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08<\/a>\u3002\u300d<br \/>\n\u807d\u5230\u9019\u500b\u5c0f\u5149\u773c\u775b\u53c8\u4eae\u4e86\u8d77\u4f86\u3002<\/p>\n<h3>Thread<\/h3>\n<p>\u8981\u4ecb\u7d39\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08\u5c31\u4e0d\u80fd\u4e0d\u63d0\u5230<a href=\"https:\/\/zh.m.wikipedia.org\/wiki\/%E7%BA%BF%E7%A8%8B\" title=\"Wiki \u57f7\u884c\u5e8f\" rel=\"nofollow noopener\" target=\"_blank\">\u57f7\u884c\u5e8f<\/a>\u9019\u6982\u5ff5\uff0c\u57f7\u884c\u5e8f\u5c31\u662f\u4f5c\u696d\u7cfb\u7d71\u900f\u904eCPU\u5728\u539f\u4f86\u7684\u7a0b\u5e8f\u4e2d\u7522\u751f\u4e00\u689d\u5e73\u884c\u7684\u7dda\u7a0b\uff0c\u4e26\u5728\u90a3\u7dda\u7a0b\u8655\u7406\u8cc7\u6599\uff0c\u7136\u5f8cC#\u7684<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/api\/system.threading.thread\" title=\"Thread \u985e\u5225\" rel=\"nofollow noopener\" target=\"_blank\">Thread<\/a>\u5c31\u662f\u5229\u7528\u4f5c\u696d\u7cfb\u7d71\u7684\u57f7\u884c\u5e8f\u4f86\u8655\u7406\u8cc7\u6599\uff0c\u7136\u5f8c\u9019\u908a\u4e0d\u591a\u505a\u8aaa\u660e\uff0c\u56e0\u70baOS\u5c64\u7d1a\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5\u6210\u672c\u8017\u8cbb\u8f03\u591a\uff0c\u518d\u4f86\u662f\u7121\u6cd5\u6709\u56de\u50b3\u503c\u4ee5\u53ca\u7121\u6cd5\u53d6\u6d88\u7b49\u56e0\u7d20\u6240\u4ee5\u4f7f\u7528\u4e0a\u6709\u8af8\u591a\u9650\u5236\uff0c\u56e0\u6b64\u4eca\u5929\u53ea\u4ecb\u7d39\u5e38\u7528\u7684<code>Task<\/code>\u3002<\/p>\n<h3>Task<\/h3>\n<p>\u95dc\u65bc<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/api\/system.threading.tasks.task\" title=\"Task \u985e\u5225\" rel=\"nofollow noopener\" target=\"_blank\">Task<\/a>\u7684\u7528\u6cd5\u6709\u5169\u7a2e\uff0c\u5176\u4e2d\u6bd4\u8f03\u820a\u7684\u5beb\u6cd5\u662f\u900f\u904e<code>Task.Factory.StartNew<\/code>\u4f86\u7522\u751f\u4e00\u500b\u65b0\u7684<code>Task<\/code>\uff0c\u4f46\u662f<a href=\"https:\/\/docs.microsoft.com\/zh-tw\/dotnet\/api\/system.threading.tasks.taskfactory.startnew\" title=\"Task.Factory.StartNew\" rel=\"nofollow noopener\" target=\"_blank\">Task.Factory.StartNew<\/a>\u4e2d\u5efa\u8b70\u9664\u975e\u9700\u8981\uff0c\u5426\u5247\u5efa\u8b70\u76f4\u63a5\u4f7f\u7528<code>Task.Run<\/code>\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u8acb\u5927\u5bb6\u770b\u4e00\u4e0b\u4ee5\u4e0b\u7bc4\u4f8b\u3002<\/p>\n<pre><code class=\"language-cs\">Console.WriteLine(&quot;Main Start Step1&quot;);\nTask.Run(() =&gt;\n{\n    Console.WriteLine(&quot;Task Start Step2&quot;);\n});\nConsole.WriteLine(&quot;Main End Step3&quot;);<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u7684\u7d50\u679c\u5982\u4f55\u5462\uff0c<code>Task.Run<\/code>\u5167\u7684\u65b9\u6cd5\u5728\u6700\u5f8c\u4e00\u500b\u57f7\u884c\u5b8c\u624d\u63a5\u8457\u57f7\u884c\uff0c\u6240\u4ee5\u7d50\u679c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-bash\">Main Start Step1\nMain End Step1\nTask Start Step2<\/code><\/pre>\n<p>\u4e0d\u904e\u9019\u6a23\u5beb\u53c8\u591a\u4e86\u5f88\u591a\u8d05\u8a5e\uff0c\u800c\u4e14\u95b1\u8b80\u4e0a\u4e0d\u662f\u90a3\u9ebc\u6e05\u695a\uff0c\u6240\u4ee5\u6700\u5f8c\u8981\u4ecb\u7d39\u8a9e\u6cd5\u7cd6<code>async<\/code>\u8ddf<code>await<\/code>\u3002<\/p>\n<h3>async await<\/h3>\n<p>\u5728\u975e\u540c\u6b65\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u6700\u5f8c\u8981\u4ecb\u7d39\u7684\u662f<code>async<\/code>\u8ddf<code>await<\/code>\uff0c\u5176\u5be6\u9019\u5169\u500b\u6771\u897f\u8ddf\u4e4b\u524d\u6240\u8aaa\u7684<code>Task<\/code>\u6c92\u6709\u885d\u7a81\uff0c\u53ea\u4e0d\u904e\u662f\u70ba\u4e86\u8b93\u7a0b\u5f0f\u8a9e\u6cd5\u5beb\u5f97\u66f4\u7c21\u6f54\u800c\u5df2\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u6211\u5011\u770b\u770b\u8981\u600e\u9ebc\u5beb\u3002<\/p>\n<pre><code class=\"language-cs\">        public async Task Print(string msg)\n        {\n            Console.WriteLine(msg);\n        }\n\n        public async Task DoPrint()\n        {\n            await Task.Delay(TimeSpan.FromSeconds(1));\n            await Print(&quot;This is message&quot;);\n        }\n\n        public async Task DoNotWait()\n        {\n            Print(&quot;This is begin&quot;);\n            DoPrint();\n            await Task.Delay(TimeSpan.FromSeconds(1));\n            await Print(&quot;This is end&quot;);\n            Console.WriteLine(&quot;This is finish&quot;);\n        }<\/code><\/pre>\n<p>\u70ba\u4e86\u8981\u51f8\u986f\u7a0b\u5e8f\u8dd1\u7684\u6642\u9593\u5e8f\u6240\u4ee5\u5728\u4f8b\u5b50\u4e2d\u505a\u4e86\u4e00\u4e9b<code>Task.Delay<\/code>\u7684\u7b49\u5f85\uff0c\u7c21\u55ae\u8aaa\u660e\u4e00\u4e0b\u5c31\u662f\u5728\u65b9\u6cd5\u4e0a\u5ba3\u544a<code>async<\/code>\u4ee3\u8868\u9019\u500b\u65b9\u6cd5\u662f\u975e\u540c\u6b65\u7684\u65b9\u6cd5\uff0c\u800c\u662f\u5426\u8981\u7b49\u5f85\u5c31\u53d6\u6c7a\u65bc\u547c\u53eb\u8a72\u65b9\u6cd5\u6642\u6709\u6c92\u6709\u52a0\u4e0a<code>await<\/code>\u7684\u4fee\u98fe\u8a5e\u3002<br \/>\n\u6240\u4ee5\u4e0a\u8ff0\u7684\u7d50\u679c\u6703\u5982\u4e0b\u5167\u5bb9\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-bash\">This is begin\nThis is end\nThis is finish\nThis is message<\/code><\/pre>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u300c\u524d\u8f29\u5b78\u4e86\u975e\u540c\u6b65\u7a0b\u5f0f\u958b\u767c\u5f8c\u8b93\u6211\u5c0d\u65bc\u7a0b\u5f0f\u958b\u767c\u6709\u591a\u4e00\u4e9b\u8a8d\u8b58\uff0c\u4f46\u662f\u6211\u89ba\u5f97\u982d\u597d\u6688\u554a\u3002\u300d<br \/>\n\u5728\u807d\u5b8c\u5927\u982d\u7684\u4ecb\u7d39\u4e4b\u5f8c\u5c0f\u5149\u9019\u9ebc\u8aaa\uff0c\u7136\u5f8c\u5927\u982d\u807d\u5b8c\u5c0f\u5149\u7684\u611f\u60f3\u5f8c\u54c8\u54c8\u5927\u7b11\u4e26\u4e14\u9019\u9ebc\u8aaa\u3002<br \/>\n\u300c\u54c8\u54c8\u54c8\uff0c\u6c92\u95dc\u4fc2\u5566\u9019\u662f\u7a0b\u5f0f\u8a2d\u8a08\u7684\u5fc5\u7d93\u4e4b\u8def\uff0c\u9047\u5230\u4e0d\u61c2\u5f8c\u628a\u4ed6\u641e\u61c2\uff0c\u63a5\u4e0b\u4f86\u8981\u8ddf\u4f60\u958b\u59cb\u4ecb\u7d39\u7db2\u9801\u7a0b\u5f0f\u958b\u767c\u56c9\uff0c\u656c\u8acb\u671f\u5f85\u5537\u3002\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u540c\u6642\u9032\u884c \u6628\u5929\u7684\u65e5\u5b50\u4e2d\u5c0f\u5149\u5b78\u5230\u4e86\u5982\u4f55\u4f7f\u7528delegate\u8ddfLinq\uff0c\u9019\u8b93\u5c0f\u5149\u5728\u8cc7\u6599\u8655\u7406\u4e2d\u7684\u7a0b\u5f0f\u958b\u767c\u5927\u5927\u9032\u6b65 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/09\/25\/async-await\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-20 \u975e\u540c\u6b65 ? async ? await\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,102,104,71,92],"class_list":["post-2176","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-async","tag-await","tag-c","tag-dotnetcore"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2176"}],"version-history":[{"count":17,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2176\/revisions"}],"predecessor-version":[{"id":2941,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2176\/revisions\/2941"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}