{"id":792,"date":"2021-09-08T11:41:13","date_gmt":"2021-09-08T03:41:13","guid":{"rendered":"http:\/\/www.wayln.com\/?p=792"},"modified":"2021-09-08T11:57:14","modified_gmt":"2021-09-08T03:57:14","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-%e5%9c%a8%e4%bb%bb%e5%8a%a1%e5%ae%8c%e6%88%90%e6%97%b6%e5%a4%84%e7%90%86%e5%ae%83%e4%bb%ac","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=792","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014\u5728\u4efb\u52a1\u5b8c\u6210\u65f6\u5904\u7406\u5b83\u4eec"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u95ee\u9898<\/a><\/li><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u89e3\u51b3\u65b9\u6848<\/a><\/li><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_1\">3<\/span> \u8ba8\u8bba<\/a><\/li><\/ul><\/div>\n<h1><span id=\"i\">\u95ee\u9898<\/span><\/h1>\n<p>\u5047\u8bbe\u6709\u4e2a\u4efb\u52a1\u96c6\u5408\u9700\u8981\u7b49\u5f85\uff0c\u800c\u5728\u6bcf\u4e2a\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u9700\u8981\u6267\u884c\u4e00\u4e9b\u5904\u7406\u3002\u4f46\u662f\uff0c\u6700\u597d\u80fd\u5728\u4efb\u52a1\u5b8c\u6210\u65f6\u5373\u53ef\u8fdb\u884c\u5904\u7406\uff0c\u800c\u4e0d\u662f\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task&lt;int&gt; DelayAndReturnAsync(int value)\n{\n    await Task.Delay(TimeSpan.FromSeconds(value));\n    return value;\n}\n\/\/\u5f53\u524d\uff0c\u6b64\u65b9\u6cd5\u6253\u5370\u201c2\u201d \u201c3\u201d \u201c1\u201d\uff0c\u7406\u60f3\u72b6\u51b5\u662f\u6253\u5370\u201c1\u201d \u201c2\u201d \u201d3\u201c\nasync Task ProcessTasksAsync()\n{\n    \/\/\u521b\u5efa\u4efb\u52a1\u5e8f\u5217\n    Task&lt;int&gt; taskA=DelayAndReturnAsync(2);\n    Task&lt;int&gt; taskB=DelyaAndReturnAsync(3);\n    Task&lt;int&gt; taskC=DelayAndReturnAsync(1);\n    Task&lt;int&gt;[] tasks=new[]{taskA,taskB,taskC};\n    \/\/\u6309\u987a\u5e8f\u7b49\u5f85\u6bcf\u4e2a\u4efb\u52a1\n    foreach(Task&lt;int&gt; task in tasks)\n    {\n        var result=await task;\n        Trace.WriteLine(result);\n    }\n\n<\/code><\/pre>\n<p>\u5f53\u524d\uff0c\u4ee3\u7801\u6309\u7167\u5e8f\u5217\u7684\u987a\u5e8f\u7b49\u5f85\u6bcf\u4e2a\u4efb\u52a1\uff0c\u5373\u4f7f\u5e8f\u5217\u4e2d\u7684\u7b2c\u4e09\u4e2a\u4efb\u52a1\u662f\u6700\u5148\u5b8c\u6210\u7684\uff0c\u4e5f\u4f1a\u5982\u6b64\u3002\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u662f\uff0c\u5728\u6bcf\u4e2a\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u53ef\u4ee5\u4e0d\u7b49\u5f85\u5176\u4ed6\u4efb\u52a1\u800c\u76f4\u63a5\u8fdb\u884c\u5904\u7406\uff0c\u6bd4\u5982Trace.WriteLine.<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u8fd9\u4e2a\u95ee\u9898\u6709\u82e5\u5e72\u4e2d\u89e3\u51b3\u65b9\u6848\u3002\u6700\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u662f\u91cd\u6784\u4ee3\u7801\uff0c\u5f15\u5165\u9ad8\u5c42\u53ca\u7684async\u65b9\u6cd5\u6765\u5904\u7406\u5bf9\u4efb\u52a1\u7684\u7b49\u5f85\u53ca\u5176\u7ed3\u679c\uff0c\u4e00\u65e6\u628a\u5904\u7406\u8fc7\u7a0b\u5206\u89e3\u51fa\u6765\uff0c\u4ee3\u7801\u5c31\u4f1a\u5927\u5927\u7b80\u5316\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task&lt;int&gt; DelayAndReturnAsync(int value)\n{\n    await Task.Delay(TimeSpan.FromSeconds(value);\n    return value;\n}\n\nasync Task AwaitAndProcessAsync(Task&lt;int&gt; task)\n{\n    int result=await task;\n    Trace.WriteLine(result);\n}\n\/\/\u73b0\u5728\u8be5\u65b9\u6cd5\u6253\u5370\u201c1\u201d \u201c2\u201d \u201c3\u201d\nasync Task ProcessTaskAsync()\n{\n    \/\/\u521b\u5efa\u4efb\u52a1\u5e8f\u5217\n    Task&lt;int&gt; taskA=DelayAndReturnAsync(2);\n    Task&lt;int&gt; taskB=DelayAndReturnAsync(3);\n    Task&lt;int&gt; taskC=DelayAndReturnAsync(1);\n    Task&lt;int&gt;[] tasks=new {taskA,taskB,taskC};\n    IEnumerable&lt;Task&gt; taskQuery=from t in tasks select AwaitAndProcessAsync(t);\n    Task[] processingTasks=taskQuery.ToArray();\n    \/\/\u7b49\u5f85\u6240\u6709\u5904\u7406\u5de5\u4f5c\u5b8c\u6210\n    await Task.WhenAll(processingTasks);\n}\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task&lt;int&gt; DelayAndReturnAsync(int value)\n{\n    await Task.Delay(TimeSpan.FromSeconds(value);\n    return value;\n}\n\n\/\/\u73b0\u5728\u8be5\u65b9\u6cd5\u6253\u5370\u201c1\u201d \u201c2\u201d \u201c3\u201d\nasync Task ProcessTaskAsync()\n{\n    \/\/\u521b\u5efa\u4efb\u52a1\u5e8f\u5217\n    Task&lt;int&gt; taskA=DelayAndReturnAsync(2);\n    Task&lt;int&gt; taskB=DelayAndReturnAsync(3);\n    Task&lt;int&gt; taskC=DelayAndReturnAsync(1);\n    Task&lt;int&gt;[] tasks=new {taskA,taskB,taskC};\n    IEnumerable&lt;Task&gt; taskQuery=tasks.Select(ansyc t=&gt;{\n        var result=await t;\n        Trace.WriteLine(result);\n    }).ToArray();\n    \/\/\u7b49\u5f85\u6240\u6709\u5904\u7406\u5de5\u4f5c\u5b8c\u6210\n    await Task.WhenAll(processingTasks);\n}\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u5982\u679c\u91cd\u6784\u4e0d\u662f\u4ee4\u4eba\u6ee1\u610f\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u90a3\u4e48\u8fd8\u6709\u4e00\u4e2a\u66ff\u4ee3\u65b9\u6848\uff0cStephen Toub\u548cJon Skeet\u90fd\u6401\u7f6e\u5f00\u53d1\u4e86\u6269\u5c55\u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u6269\u5c55\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u6309\u987a\u5e8f\u5b8c\u6210\u7684\u4efb\u52a1\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u5047\u8bbe\u6709\u4e2a\u4efb\u52a1\u96c6\u5408\u9700\u8981\u7b49\u5f85\uff0c\u800c\u5728\u6bcf\u4e2a\u4efb\u52a1\u5b8c\u6210\u65f6\uff0c\u9700\u8981\u6267\u884c\u4e00\u4e9b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,41,2],"tags":[],"class_list":["post-792","post","type-post","status-publish","format-standard","hentry","category-c","category-41","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/792","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=792"}],"version-history":[{"count":2,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions"}],"predecessor-version":[{"id":794,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/792\/revisions\/794"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}