{"id":788,"date":"2021-09-08T10:42:44","date_gmt":"2021-09-08T02:42:44","guid":{"rendered":"http:\/\/www.wayln.com\/?p=788"},"modified":"2021-09-08T10:42:44","modified_gmt":"2021-09-08T02:42:44","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-%e7%ad%89%e5%be%85%e4%b8%80%e7%bb%84%e4%bb%bb%e5%8a%a1%e5%ae%8c%e6%88%90","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=788","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014\u7b49\u5f85\u4e00\u7ec4\u4efb\u52a1\u5b8c\u6210"},"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>\u624b\u5934\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u9700\u8981\u7b49\u5f85\u4ed6\u4eec\u53bb\u5168\u90e8\u5b8c\u6210<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6846\u67b6\u63d0\u4f9b\u4e86Task.WhenAll\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u6536\u82e5\u5e72\u4e2a\u4efb\u52a1\u5e76\u5728\u8fd9\u4e9b\u4efb\u52a1\u5b8c\u6210\u540e\u8fd4\u56de\u4e00\u4e2a\u5df2\u5b8c\u6210\u7684\u4efb\u52a1<\/p>\n<pre><code class=\"language-CSharp line-numbers\">Task task1=Task.Delay(TimeSpan.FromSecond(1));\nTask task2=Task.Delay(TimeSpan.FromSecond(1));\nTask task3=Task.Delay(TimeSpan.FromSecond(1));\nawait Task.WhenAll(task1,task2,task3);\n<\/code><\/pre>\n<p>\u5982\u679c\u6240\u6709\u4efb\u52a1\u6709\u8fd9\u76f8\u540c\u7684\u7ed3\u679c\u7c7b\u578b\uff0c\u5e76\u4e14\u5747\u5df2\u6210\u529f\u5b8c\u6210\uff0c\u90a3\u4e48Task.WhenAll\u4f1a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u62ec\u6240\u6709\u4efb\u52a1\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">Task&lt;int&gt; task1=Task.FromResult(3);\nTask&lt;int&gt; task2=Task.FromResult(5);\nTask&lt;int&gt; task3=Task.FromResult(7);\nint[] results=await Task.WhenAll(task1,task2,task3);\n\/\/\u7ed3\u679c\u5305\u542b{3,5,7}\n<\/code><\/pre>\n<p>Task.WhenAll\u6709\u4e00\u4e2a\u91cd\u8f7d\uff0c\u4f1a\u63a5\u6536IEnumerable\u4e2d\u7684\u4efb\u52a1\uff0c\u4f46\u662f\u6700\u597d\u4e0d\u8981\u4f7f\u7528\u8fd9\u4e2a\u91cd\u8f7d\u3002\u4e00\u65e6\u628a\u5f02\u6b65\u4ee3\u7801\u4e0eLINQ\u6df7\u5408\u5728\u4e00\u8d77\uff0c\u7136\u540e\u5c06\u5e8f\u5217\u663e\u5f0f\u5730\u5177\u8c61\u5316\uff08\u6bd4\u5982\u5bf9\u5e8f\u5217\u6c42\u503c\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u96c6\u5408\uff09\uff0c\u4ee3\u7801\u5c31\u4f1a\u53d8\u5f97\u66f4\u4e3a\u6e05\u6670\u3002<\/p>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u5982\u6709\u4efb\u4f55\u4efb\u52a1\u629b\u51fa\u5f02\u5e38\uff0cTask.WhenAll\u90fd\u4f1a\u5c06\u5176\u8fd4\u56de\u7684\u4efb\u52a1\u4e0e\u8be5\u5f02\u5e38\u4e00\u540c\u5f53\u505a\u9519\u8bef\u5904\u7406\u3002\u5982\u679c\u591a\u4e2a\u4efb\u52a1\u629b\u51fa\u5f02\u5e38\uff0c\u90a3\u4e48\u6240\u6709\u8fd9\u4e9b\u5f02\u5e38\u90fd\u4f1a\u88ab\u9632\u6b62\u5728\u7531Task.WhenAll\u8fd4\u56de\u7684Task\u4e2d\u3002\u4f46\u662f\uff0c\u5f53\u8be5\u4efb\u52a1\u5c1a\u5728\u7b49\u5f85\u4e2d\u65f6\uff0c\u4fbf\u53ea\u4f1a\u629b\u51fa\u5176\u4e2d\u7684\u4e00\u4e2a\u5f02\u5e38\u3002\u5982\u679c\u68c0\u67e5Task.WhenAll\u8fd4\u56de\u7684Task\u7684Exception\u5c5e\u6027\uff0c\u5219\u53ef\u83b7\u5f97\u6bcf\u4e00\u4e2a\u7279\u5b9a\u7684\u5f02\u5e38<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task ThrowNotImplementedExceptionAsync()\n{\n    throw new NotImplementedException();\n}\nasync Task ThrowInvalidOperationExceptionAsync()\n{\n    throw new InvalidOperationException();\n}\nasync Task ObserveOneExceptionAsync()\n{\n    var task1=ThrowNotImplementedExceptionAsync();\n    var task2=ThrowInvalidOperationExceptionAsync();\n    try\n    {\n        await Task.WhenAll(task1,task2);\n    }\n    cath(Exception ex)\n    {\n        \/\/ex\u4e0d\u662fNotImplementedException\u5c31\u662fInvalidOperationException\n        ...\n    }\n}\nasync Task ObserveAllExceptionsAsync()\n{\n    var task1=ThrowNotImplementedExceptionAsync();\n    var task2=ThrowInvalidOperationExceptionAsync();\n    Task allTasks=Task.WhenAll(task1,task2);\n    try\n    {\n        await allTasks;\n    }\n    catch\n    {\n        AggregateException allExceptions=allTasks.Exception;\n        ...\n    }\n}\n\n<\/code><\/pre>\n<p>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528Task.WhenAll\u65e0\u9700\u89c2\u5bdf\u6240\u6709\u5f02\u5e38\uff0c\u901a\u5e38\u53ea\u9700\u8981\u54cd\u5e94\u7b2c\u4e00\u4e2a\uff08\u800c\u975e\u6240\u6709\uff09\u629b\u51fa\u7684\u5f02\u5e38\u3002<br \/>\n\u6ce8\u610f\uff1a\u5728\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0cThrowNotImplementedExceptionAsync\u65b9\u6cd5\u548cThrowInvalidOperationExceptionAsync\u65b9\u6cd5\u5e76\u6ca1\u6709\u76f4\u63a5\u629b\u51fa\u5404\u81ea\u7684\u5f02\u5e38\u3002\u56e0\u4e3a\u4ed6\u4eec\u90fd\u4f7f\u7528\u4e86Async\u5173\u952e\u5b57\uff0c\u6240\u4ee5\u5f53\u4ed6\u4eec\u7684\u5f02\u5e38\u88ab\u6355\u83b7\u540e\uff0c\u4f1a\u88ab\u653e\u7f6e\u5728\u4e00\u4e2a\u6b63\u5e38\u8fd4\u56de\u7684\u4efb\u52a1\u4e2d\uff0c\u5bf9\u8fd4\u56de\u7b49\u5f85\u7c7b\u578b\u7684\u65b9\u6cd5\u800c\u8a00\uff0c\u8fd9\u79cd\u60c5\u51b5\u662f\u5b8c\u5168\u6b63\u5e38\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u624b\u5934\u6709\u4e00\u4e9b\u4efb\u52a1\uff0c\u9700\u8981\u7b49\u5f85\u4ed6\u4eec\u53bb\u5168\u90e8\u5b8c\u6210 \u89e3\u51b3\u65b9\u6848 \u4e3a\u4e86\u89e3\u51b3 [&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-788","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\/788","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=788"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/788\/revisions"}],"predecessor-version":[{"id":789,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/788\/revisions\/789"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}