{"id":783,"date":"2021-09-07T22:43:40","date_gmt":"2021-09-07T14:43:40","guid":{"rendered":"http:\/\/www.wayln.com\/?p=783"},"modified":"2021-09-07T22:43:40","modified_gmt":"2021-09-07T14:43:40","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80-%e6%9a%82%e5%81%9c%e4%b8%80%e6%ae%b5%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=783","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u57fa\u7840\u2014\u2014\u6682\u505c\u4e00\u6bb5\u65f6\u95f4"},"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>\u7a0b\u5e8f\u9700\u8981\u5f02\u6b65\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u3002\u5728\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u6216\u5b9e\u73b0\u91cd\u8bd5\u5ef6\u65f6\u65f6\uff0c\u8fd9\u662f\u4e00\u79cd\u5e38\u89c1\u573a\u666f<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u5728Task\u7c7b\u578b\u4e2d\u6709\u4e2aDelay\u9759\u6001\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5728\u6307\u5b9a\u65f6\u95f4\u7ed3\u675f\u540e\u8fd4\u56de\u5df2\u5b8c\u6210\u7684\u4efb\u52a1<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task&lt;T&gt; DelayResult&lt;T&gt;(T result,TimeSpan delay)\n{\n    await Task.Delay(delay);\n    return result;\n}\n<\/code><\/pre>\n<p><strong>\u5bf9\u4e8e\u751f\u4ea7\u4ee3\u7801\uff0c\u63a8\u8350\u4f7f\u7528\u66f4\u4e3a\u5f7b\u5e95\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u5982Polly NuGet\u5e93<\/strong><\/p>\n<p>Task.Delay\u8fd8\u53ef\u7528\u4f5c\u7b80\u5355\u7684\u8d85\u65f6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528CancellationTokenSource\u8fd9\u79cd\u7c7b\u578b\u6765\u5b9e\u73b0\u8d85\u65f6\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u7528\u65e0\u9650\u4efb\u52a1\uff08Task.Delay)\u6765\u5305\u88c5\u53d6\u6d88\u4ee4\u724c\uff0c\u4ece\u800c\u63d0\u4f9b\u5728\u6307\u5b9a\u65f6\u95f4\u4e4b\u540e\u53d6\u6d88\u7684\u4efb\u52a1\uff0c\u7136\u540e\uff0c\u628a\u90a3\u4e2a\u8ba1\u65f6\u4efb\u52a1\u914d\u5408Task.WhenAny\u4e00\u8d77\u4f7f\u7528\uff0c\u5b9e\u73b0\u8f6f\u8d85\u65f6<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task&lt;String&gt; DownloadStringWithTimeout(HttpClient client,string uri)\n{\n    using var cts=new CancellationTokenSource(TimeSpan.FromSecond(3));\n    Task&lt;String&gt; downloadTask=client.GetStringAsync(uri);\n    Task timeoutTask=Task.Delay(Timout.InfiniteTimeSpan,cts,Token);\n    Task completedTask=await Task.WhenAny(downloadTask,timeoutTask);\n    if(completedTask==timeoutTask)\n    {\n        return null;\n    }\n    return await downloadTask;\n}\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u5bf9\u5f02\u6b65\u4ee3\u7801\u7684\u5355\u5143\u6d4b\u8bd5\u6216\u91cd\u8bd5\u903b\u8f91\u7684\u5b9e\u73b0\u6765\u8bf4\uff0cTask.Delay\u662f\u5f88\u597d\u7684\u9009\u62e9\u3002\u7136\u800c\uff0c\u5982\u679c\u8981\u5b9e\u73b0\u8d85\u65f6\uff0cCancellationToken\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u7a0b\u5e8f\u9700\u8981\u5f02\u6b65\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u3002\u5728\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u6216\u5b9e\u73b0\u91cd\u8bd5\u5ef6\u65f6\u65f6\uff0c [&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-783","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\/783","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=783"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions"}],"predecessor-version":[{"id":784,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/783\/revisions\/784"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}