{"id":797,"date":"2021-09-08T14:57:12","date_gmt":"2021-09-08T06:57:12","guid":{"rendered":"http:\/\/www.wayln.com\/?p=797"},"modified":"2021-09-08T14:57:12","modified_gmt":"2021-09-08T06:57:12","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-async-task%e6%96%b9%e6%b3%95%e7%9a%84%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=797","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014async Task\u65b9\u6cd5\u7684\u5f02\u5e38\u5904\u7406"},"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>\u5f02\u5e38\u5904\u7406\u5bf9\u6240\u6709\u8bbe\u8ba1\u6765\u8bf4\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u73af\u3002\u9488\u5bf9\u6210\u529f\u60c5\u51b5\u7684\u8bbe\u8ba1\u5b9e\u73b0\u8d77\u6765\u7b80\u5355\uff0c\u4f46\u53ea\u6709\u628a\u5931\u8d25\u60c5\u51b5\u4e5f\u5904\u7406\u5f97\u5f53\uff0c\u624d\u7b97\u8bbe\u8ba1\u6b63\u786e\uff0c\u5e78\u8fd0\u7684\u662f\uff0casync Task\u65b9\u6cd5\u9488\u5bf9\u5f02\u5e38\u7684\u5904\u7406\u4e5f\u662f\u76f4\u63a5\u4e86\u5f53\u7684\u3002<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u5f02\u5e38\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684try\/catch\u6765\u6355\u83b7\uff0c\u5c31\u50cf\u5904\u7406\u540c\u6b65\u4ee3\u7801\u4e00\u6837\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task ThrowExceptionAsync()\n{\n    await Task.Delay(TimeSpan.FromSeconds(1));\n    throw new InvalidOperationException(\"Test\");\n}\nasync Task TestAsync()\n{\n    try\n    {\n        await ThrowExceptionAsync();\n    }\n    catch(InvalidOperationException)\n    {\n\n    }\n}\n\n<\/code><\/pre>\n<p>async Task\u65b9\u6cd5\u5f15\u53d1\u7684\u5f02\u5e38\u88ab\u653e\u7f6e\u5728\u8fd4\u56de\u7684Task\u4e2d\uff0c\u53ea\u6709\u5728\u7b49\u5f85\u8fd4\u56de\u7684Task\u65f6\uff0c\u4ed6\u4eec\u624d\u4f1a\u89e6\u53d1\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task ThrowExceptionAsync()\n{\n    await Task.Delay(TimeSpan.FromSeconds(1));\n    throw new InvalidOperationException(\"Test\");\n}\nasync Task TestAsync()\n{\n    \/\/\u4ece\u65b9\u6cd5\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u88ab\u7f6e\u4e8e\u4efb\u52a1\u4e2d\n    Task task=ThrowExceptionAsync();\n    try\n    {\n        \/\/\u5728\u7b49\u5f85\u65f6\uff0c\u5f02\u5e38\u518d\u6b21\u89e6\u53d1\n        await task;\n    }\n    catch(InvalidOperationException)\n    {\n        \/\/\u5728\u6b64\u5904\u6b63\u786e\u6355\u6349\u5f02\u5e38\n    }\n}\n\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u5f53\u5f02\u5e38\u4eceasync Task\u65b9\u6cd5\u4e2d\u629b\u51fa\u65f6\uff0c\u4ed6\u4f1a\u88ab\u6355\u6349\u5e76\u653e\u7f6e\u5728\u5728\u8fd4\u56de\u7684Task\u4e2d\uff0c\u7531\u4e8easync void \u65b9\u6cd5\u5e76\u6ca1\u6709\u7528\u6765\u653e\u7f6e\u5f02\u5e38\u7684Task,\u56e0\u6b64\u8fd9\u4e9b\u65b9\u6cd5\u7684\u884c\u4e3a\u6709\u9519\u4e0d\u540c\u3002<br \/>\n\u5728\u5bf9\u51fa\u9519\u7684Task\u52a0\u4ee5await\u65f6\uff0c\u8be5\u4efb\u52a1\u7684\u7b2c\u4e00\u4e2a\u5f02\u5e38\u4f1a\u88ab\u518d\u6b21\u629b\u51fa\u3002<br \/>\n\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4ee3\u7801\u5e94\u5c06\u5f02\u5e38\u4ece\u5176\u8c03\u7528\u7684\u5f02\u6b65\u65b9\u6cd5\u4e0a\u5f00\u59cb\u4f20\u64ad\uff0c\u5176\u5fc5\u884c\u4e4b\u4e8b\u53ea\u662fawait\u4ece\u5f02\u6b65\u65b9\u6cd5\u4e2d\u8fd4\u56de\u7684\u4efb\u52a1\u800c\u5df2\uff0c\u800c\u5f02\u5e38\u4fbf\u4f1a\u81ea\u7136\u5730\u4f20\u64ad\u3002<br \/>\n\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6bd4\u5982Task.WhenAll,\u4e00\u4e2aTask\u53ef\u80fd\u6709\u591a\u4e2a\u5f02\u5e38\uff0c\u800cawait\u53ea\u4f1a\u518d\u6b21\u629b\u51fa\u7b2c\u4e00\u4e2a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u5f02\u5e38\u5904\u7406\u5bf9\u6240\u6709\u8bbe\u8ba1\u6765\u8bf4\u90fd\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u73af\u3002\u9488\u5bf9\u6210\u529f\u60c5\u51b5\u7684\u8bbe [&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-797","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\/797","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=797"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/797\/revisions"}],"predecessor-version":[{"id":798,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/797\/revisions\/798"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}