{"id":799,"date":"2021-09-08T15:50:07","date_gmt":"2021-09-08T07:50:07","guid":{"rendered":"http:\/\/www.wayln.com\/?p=799"},"modified":"2021-09-08T15:50:07","modified_gmt":"2021-09-08T07:50:07","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-async-void%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=799","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014async void\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>\u5f53\u6709\u5f02\u5e38\u4eceasync void \u65b9\u6cd5\u4e2d\u4f20\u64ad\u51fa\u6765\u65f6\uff0c\u9700\u4e88\u4ee5\u5904\u7406\u3002<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u5982\u679c\u53ef\u4ee5\uff0c\u628a\u65b9\u6cd5\u7684\u8fd4\u56de\u5bf9\u8c61\u4ecevoid\u6539\u4e3aTask,\u4e0d\u8fc7\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5e76\u4e0d\u53ef\u884c\u3002\u6bd4\u5982\u9700\u8981\u5bf9ICommand\u5b9e\u73b0\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\uff0c\u6b64\u65f6\u5fc5\u987b\u8fd4\u56devoid.\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u63d0\u4f9bExecute\u65b9\u6cd5\u7684Task\u8fd4\u56de\u91cd\u8f7d<\/p>\n<pre><code class=\"language-CSharp line-numbers\">sealed class MyAsyncCommand:ICommand\n{\n    async void ICommand.Execute(object parameter)\n    {\n        await Execute(parameter);\n    }\n    public async Task Execute(object parameter)\n    {\n        ...\/\/\u6b64\u5904\u4e3a\u5f02\u6b65\u547d\u4ee4\u7684\u5b9e\u73b0\n    }\n    ...\/\/\u5176\u4ed6\u90e8\u5206\uff08CanExecute\u7b49\uff09\n\n}\n<\/code><\/pre>\n<p>\u6700\u597d\u907f\u514d\u8ba9\u5f02\u5e38\u4eceasync void\u65b9\u6cd5\u4e2d\u4f20\u64ad\u51fa\u53bb\uff0c\u5982\u679c\u5fc5\u987b\u4f7f\u7528async void \u65b9\u6cd5\uff0c\u5c31\u9700\u8981\u8003\u8651\u5c06\u5b83\u7684\u6574\u4e2a\u4ee3\u7801\u5305\u88c5\u5728try\u4ee3\u7801\u5757\u4e2d\uff0c\u76f4\u63a5\u5904\u7406\u5f02\u5e38\u3002<br \/>\n\u5173\u4e8easync void\u65b9\u6cd5\u6c47\u603b\u7684\u5f02\u5e38\u5904\u7406\uff0c\u8fd8\u6709\u53e6\u4e00\u79cd\u65b9\u6848\u3002\u5982\u679casync void\u65b9\u6cd5\u4f20\u64ad\u5f02\u5e38\uff0c\u5728\u8be5\u65b9\u6cd5\u5f00\u59cb\u6267\u884c\u7684\u540c\u4e8b\uff0c\u4e00\u573a\u4f1a\u88ab\u629b\u5230\u5f53\u65f6\u6b63\u5728\u6d3b\u52a8\u7684SynchronizationContext\u4e0a\uff0c\u5982\u679c\u6267\u884c\u73af\u5883\u63d0\u4f9b\u4e86SynchronizationContext\uff0c\u90a3\u4e48\u901a\u5e38\u6709\u529e\u6cd5\u5728\u5168\u5267\u7ec8\u5904\u7406\u8fd9\u4e9b\u9876\u5c42\u7684\u5f02\u5e38\uff0c\u6bd4\u5982WPF\u6709Application.DispatcherUnhandledException\uff0cUWP\u6709Application.UnhandledException,ASP.NET\u5219\u6709UseExceptionHandler\u4e2d\u95f4\u4ef6\u3002<br \/>\n\u6b64\u5916\uff0c\u901a\u8fc7\u63a7\u5236SynchronizationContext\uff0c\u4e5f\u53ef\u4ee5\u5904\u7406async void \u65b9\u6cd5\u4e2d\u7684\u5f02\u5e38\u3002\u81ea\u884c\u7f16\u5199SynchronizationContext\u5e76\u975e\u6613\u4e8b\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u514d\u8d39\u7684Nito.AsyncExNuGet helper\u5e93\u4e2dAsyncContext\u7c7b\u578b\u3002\u5bf9\u4e8e\u6ca1\u6709\u5185\u7f6eSynchronizationContext\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u6bd4\u5982Win32\u670d\u52a1\u548c\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\uff0cAsyncContext\u5c24\u4e3a\u6709\u7528\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u4f7f\u7528\u4e86AsyncContext\u6765\u8fd0\u884c\u5e76\u5904\u7406async void \u65b9\u6cd5\u4e2d\u7684\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">static class Program\n{\n    static void Main(string[] args)\n    {\n        try\n        {\n            AsyncContext.Run(()=&gt;MainAsync(args));\n        }\n        catch(Exception ex)\n        {\n            Console.Error.WriteLine(ex);\n        }\n    }\n    \/\/\u8fd9\u79cd\u4ee3\u7801\u5f88\u5dee\u52b2\uff01\n    \/\/\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u82e5\u975e\u5fc5\u8981\uff0c\u5426\u5219\u5e94\u907f\u514d\u4f7f\u7528async void\n    static async void MainAsync(string[] args)\n    {\n        ...\n    }\n}\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u8fd4\u56deTask\u7684\u65b9\u6cd5\u66f4\u5bb9\u6613\u6d4b\u8bd5\uff0c\u8fd9\u4e5f\u662fasync Task\u6bd4async void\u66f4\u53d7\u9752\u7750\u7684\u539f\u56e0\u4e4b\u4e00\uff0c\u7528\u8fd4\u56deTask\u7684\u65b9\u6cd5\u6765\u91cd\u8f7d\u8fd4\u56devoid\u7684\u65b9\u6cd5\uff0c\u81f3\u5c11\u80fd\u4f53\u7528\u53ef\u6d4b\u8bd5\u7684API\u63a5\u53e3\u3002<br \/>\n\u5982\u679c\u9700\u8981\u63d0\u4f9b\u81ea\u5df1\u7684SynchronizationContext\u7c7b\u578b\uff08\u6bd4\u5982AsyncContext)\uff0c\u5207\u52ff\u5c06SynchronizationContext\u653e\u5230\u4efb\u4f55\u5e76\u4e0d\u4e3a\u4f60\u6240\u7528\u7684\u7ebf\u7a0b\u4e2d\u3002\u8fd9\u662f\u57fa\u672c\u7684\u51c6\u5219\uff0c\u4e0d\u5e94\u5c06\u6b64\u7c7b\u578b\u653e\u5230\u4efb\u4f55\u5df2\u7ecf\u5177\u5907\u8fd9\u79cd\u7c7b\u578b\u7684\u7ebf\u7a0b\u4e2d\uff0c\u6bd4\u5982UI\u7ebf\u7a0b\u6216ASP.NET\u7ecf\u5178\u8bf7\u6c42\u7ebf\u7a0b\uff0c\u4e5f\u4e0d\u5e94\u5c06\u6b64\u7c7b\u578b\u653e\u5230\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e2d\u3002\u4f46\u662f\uff0c\u5728\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u7ebf\u7a0b\u4ee5\u53ca\u6240\u6709\u7531\u4f60\u624b\u52a8\u521b\u5efa\u7684\u73b0\u5448\u4e0a\u90fd\u53ef\u4ee5\u5b89\u88c5\u3002<br \/>\n<strong>AsyncContext\u7c7b\u578b\u53ef\u4ee5\u5728Nito.AsyncEx NuGet\u5305\u4e2d\u83b7\u53d6<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u5f53\u6709\u5f02\u5e38\u4eceasync void \u65b9\u6cd5\u4e2d\u4f20\u64ad\u51fa\u6765\u65f6\uff0c\u9700\u4e88\u4ee5\u5904 [&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-799","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\/799","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=799"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions"}],"predecessor-version":[{"id":800,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/799\/revisions\/800"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}