{"id":785,"date":"2021-09-07T23:03:26","date_gmt":"2021-09-07T15:03:26","guid":{"rendered":"http:\/\/www.wayln.com\/?p=785"},"modified":"2021-09-07T23:03:26","modified_gmt":"2021-09-07T15:03:26","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-%e8%bf%94%e5%9b%9e%e5%b7%b2%e5%ae%8c%e6%88%90%e7%9a%84%e4%bb%bb%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=785","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014\u8fd4\u56de\u5df2\u5b8c\u6210\u7684\u4efb\u52a1"},"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>\u901a\u8fc7\u5f02\u6b65\u7b7e\u540d\u5b9e\u73b0\u540c\u6b65\u65b9\u6cd5\uff0c\u5982\u679c\u5728\u7ee7\u627f\u5f02\u6b65\u63a5\u53e3\u6216\u57fa\u672c\u7c7b\u7684\u540c\u65f6\u53c8\u60f3\u540c\u6b65\u5b9e\u73b0\u8be5\u4efb\u52a1\uff0c\u53d8\u53ef\u80fd\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u3002\u5f53\u9700\u8981\u5f02\u6b65\u63a5\u53e3\u7684\u7b80\u5355\u5b58\u6839\u6216\u6a21\u62df\u5bf9\u8c61\u65f6\uff0c\u6216\u8005\u5f53\u9700\u8981\u5bf9\u5f02\u6b65\u4ee3\u7801\u5de6\u5355\u5143\u6d4b\u8bd5\u65f6\uff0c\u8fd9\u9879\u6280\u672f\u7279\u522b\u6709\u7528\u3002<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u4f7f\u7528<span class=\"katex math inline\">Task.FromResult<\/span>\u6765\u521b\u5efa\u5e76\u8fd4\u56de\u65b0\u7684<span class=\"katex math inline\">Task&lt;T&#62;<\/span>,\u8fd9\u4e2a<span class=\"katex math inline\">Task&lt;T&#62;<\/span>\u5df2\u901a\u8fc7\u6307\u5b9a\u7684\u503c\u5b8c\u6210\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">interface IMyAsyncInterface\n{\n    Task&lt;int&gt; GetValueAsync();\n}\nclass MySynchronousImplementation:IMyAsyncInterface\n{\n    public Task&lt;int&gt; GetValueAsync()\n    {\n        return Task.FromResult(13);\n    }\n}\n\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u65e0\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u7528Task.CompletedTask\uff0c\u8fd9\u662f\u4e00\u4e2a\u5df2\u7ecf\u6210\u529f\u5b8c\u6210\u7684\u7f13\u5b58\u4efb\u52a1\uff1b<\/p>\n<pre><code class=\"language-CSharp line-numbers\">interface IMyAsyncInterface\n{\n    Task DoSomethingAsync();\n}\nclass MySynchronousImplementation:IMyAsyncInterface\n{\n    public Task  DoSomethingAsync()\n    {\n        return Task.CompletedTask;\n    }\n}\n\n<\/code><\/pre>\n<p>Task.FromResult\u53ea\u4e3a\u6210\u529f\u7684\u7ed3\u679c\u63d0\u4f9b\u5df2\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u82e5\u9700\u8981\u5177\u6709\u4e0d\u540c\u7ed3\u679c\u7684\u4efb\u52a1\uff08\u6bd4\u5982\u901a\u8fc7NotImplementedException\u5b8c\u6210\u4efb\u52a1\uff09\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528Task.FromException:<\/p>\n<pre><code class=\"language-CSharp line-numbers\">Task&lt;T&gt; NotImplementedAsync&lt;T&gt;()\n{\n    return Task.FromException&lt;T&gt;(new NotImplementedException());\n}\n<\/code><\/pre>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5df2\u7ecf\u4ece\u7ed9\u5b9a\u7684CancellationToken\u4e2d\u53d6\u6d88\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528Task.FromCanceled\u521b\u5efa\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">Task&lt;int&gt; GetValueAsync(CancellationToken cancellationToken)\n{\n    if(cancellationToken.IsCancellationRequested)\n    {\n        return Task.FromCanceled&lt;int&gt;(cancellationToken);\n     }\n    return Task.FromResult(13);\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u540c\u6b65\u5b9e\u73b0\u53ef\u80fd\u4f1a\u5931\u8d25\uff0c\u5c31\u5e94\u5f53\u6355\u6349\u5f02\u5e38\u5e76\u901a\u8fc7Task.FromException\u6765\u8fd4\u56de\u5b83\u4eec\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">interface IMyAsyncInterface\n{\n    Task DoSomethingAsync();\n}\nclass MySynchronousImplementation:IMyAsyncInterface\n{\n    public Task  DoSomethingAsync()\n    {\n        try\n        {\n            DoSomethingSynchronously();\n            return Task.CompletedTask;\n        }\n        catch(Exception ex)\n        {\n            return Task.FromException(ex);\n        }\n\n    }\n}\n\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u82e5\u8981\u901a\u8fc7\u540c\u6b65\u4ee3\u7801\u6765\u5b9e\u73b0\u5f02\u6b65\u63a5\u53e3\uff0c\u9700\u8981\u907f\u514d\u4efb\u4f55\u5f62\u5f0f\u7684\u963b\u585e\u3002\u5f53\u65b9\u6cd5\u53ef\u4ee5\u5f02\u6b65\u5b9e\u73b0\u65f6\uff0c\u963b\u585e\u5f02\u6b65\u65b9\u6cd5\u7136\u540e\u8fd4\u56de\u5df2\u5b8c\u6210\u7684\u4efb\u52a1\uff0c\u8fd9\u79cd\u60c5\u51b5\u5f88\u4e0d\u7406\u60f3<br \/>\n\u5982\u679c\u7ecf\u5e38\u4ee5\u540c\u4e00\u4e2a\u503c\u4f7f\u7528Task.FromResult,\u90a3\u4e48\u53ef\u4ee5\u8003\u8651\u7f13\u5b58\u5b9e\u9645\u4efb\u52a1\u3002\u6bd4\u5982\uff0c\u82e5\u4ee50\u4e3a\u7ed3\u679c\u521b\u5efa\u4e00\u6b21Task&lt;int&#62;\uff0c\u5c31\u907f\u514d\u4e86\u521b\u5efa\u66f4\u591a\u5fc5\u987b\u4e88\u4ee5\u5783\u573e\u56de\u6536\u7684\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">private static readonly Task&lt;int&gt; zeroTask=Task.FromResult(0);\nTask&lt;int&gt; GetValueAsync()\n{\n    return zeroTask;\n}\n<\/code><\/pre>\n<p>\u4ece\u903b\u8f91\u4e0a\u6765\u8bb2\uff0cTask.FromResult\u3001Task.FromException\u548cTask.FromCanceled\u90fd\u662f\u8f85\u52a9\u65b9\u6cd5\uff0c\u4e5f\u90fd\u662f\u901a\u7528\u7684TaskCompletionSource&lt;T&#62;\u7684\u5feb\u6377\u65b9\u5f0f\u3002\u901a\u5e38\u6765\u8bf4\uff0c\u82e5\u8981\u8fd4\u56de\u5df2\u5b8c\u6210\u7684\u4efb\u52a1\uff0c\u5e94\u5f53\u4f7f\u7528Task.FromResult\u3001Task.FromException\u548cTask.FromCanceled\u3002\u4f7f\u7528TaskCompletionSource&lt;T&#62;\u53ef\u4ee5\u8fd4\u56de\u5728\u672a\u6765\u67d0\u4e2a\u65f6\u523b\u5b8c\u6210\u7684\u4efb\u52a1<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u901a\u8fc7\u5f02\u6b65\u7b7e\u540d\u5b9e\u73b0\u540c\u6b65\u65b9\u6cd5\uff0c\u5982\u679c\u5728\u7ee7\u627f\u5f02\u6b65\u63a5\u53e3\u6216\u57fa\u672c\u7c7b\u7684\u540c\u65f6 [&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-785","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\/785","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=785"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/785\/revisions"}],"predecessor-version":[{"id":786,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/785\/revisions\/786"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}