{"id":833,"date":"2021-09-13T09:19:02","date_gmt":"2021-09-13T01:19:02","guid":{"rendered":"http:\/\/www.wayln.com\/?p=833"},"modified":"2021-09-13T09:19:02","modified_gmt":"2021-09-13T01:19:02","slug":"%e5%bc%82%e6%ad%a5%e6%b5%81-%e5%bc%82%e6%ad%a5%e6%b5%81%e5%8f%8a%e5%85%b6%e5%8f%96%e6%b6%88%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=833","title":{"rendered":"\u5f02\u6b65\u6d41\u2014\u2014\u5f02\u6b65\u6d41\u53ca\u5176\u53d6\u6d88\u64cd\u4f5c"},"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>\u5047\u8bbe\u9700\u8981\u53d6\u6d88\u5f02\u6b65\u6d41\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f<\/p>\n<h1><span id=\"i-2\">\u89e3\u51b3\u65b9\u6848<\/span><\/h1>\n<p>\u5e76\u975e\u6240\u6709\u7684\u5f02\u6b65\u6d41\u90fd\u9700\u8981\u53d6\u6d88\u3002\u5f53\u6ee1\u8db3\u6761\u4ef6\u65f6\u7b80\u5355\u5730\u505c\u6b62\u679a\u4e3e\u4e5f\u662f\u53ef\u884c\u7684\u3002\u5982\u4e0b\u6240\u793a\uff0c\u5982\u679c\u8fd9\u79cd\u7c7b\u578b\u7684\u201c\u53d6\u6d88\u201d\u53ef\u4ee5\u6ee1\u8db3\u9700\u8981\uff0c\u4fbf\u4e0d\u518d\u9700\u8981\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u53d6\u6d88\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">await foreach(int result in SlowRange())\n{\n    Console.WriteLine(result);\n    if(result&gt;=8)\n    {\n        break;\n    }\n}\nasync IAsyncEnumerable&lt;int&gt; SlowRange()\n{\n    for(int i=0;i!=10;i++)\n    {\n        await Task.Delay(i*100);\n        yield return i;\n    }\n}\n<\/code><\/pre>\n<p>\u53d6\u6d88\u5f02\u6b65\u6d41\u5f80\u5f80\u662f\u6709\u7528\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u67d0\u4e9b\u8fd0\u7b97\u7b26\u4f1a\u5bf9\u4ed6\u4eec\u7684\u6e90\u6d41\u4f20\u9012\u53d6\u6d88\u4ee4\u724c\uff0c\u5c31\u9700\u8981\u7528CancellationToken\u4ece\u5916\u90e8\u505c\u6b62 wawait foreach<br \/>\n\u901a\u8fc7\u5b9a\u4e49\u6807\u8bb0\u4e86EnumeratorCancellation\u5c5e\u6027\u7684\u53c2\u6570\uff0c\u8fd4\u56deIAsyncEnumerable&lt;T&#62;\u7684async\u65b9\u6cd5\u4e5f\u8bb8\u53ef\u4ee5\u63a5\u6536\u53d6\u6d88\u4ee4\u724c<\/p>\n<pre><code class=\"language-CSharp line-numbers\">using var cts=new CancellationTokenSource(500);\nCancellationToken token=cts.Token;\nawait foreach(int result in SlowRange(token))\n{\n    Console.WriteLine(result);\n}\nasync IAsyncEnumerable&lt;int&gt; SlowRange([EnumeratorCancellation]CancellationToken token=default)\n{\n    for(int i=0;i!=10;i++)\n    {\n        await Task.Delay(i*100,token);\n        yield resturn i;\n    }\n}\n\n<\/code><\/pre>\n<h1><span id=\"i-3\">\u8ba8\u8bba<\/span><\/h1>\n<p>\u8fd9\u91cc\u7684\u89e3\u51b3\u65b9\u6848\u5c06CancellationToken\u76f4\u63a5\u4f20\u7ed9\u4e86\u8fd4\u56de\u5f02\u6b65\u679a\u4e3e\u7684\u65b9\u6cd5\uff0c\u8fd9\u662f\u5f88\u5e38\u89c1\u7684\u7528\u6cd5\u3002<br \/>\n\u5728\u5176\u4ed6\u573a\u666f\u4e2d\uff0c\u4ee3\u7801\u4f1a\u53d7\u5230\u5f02\u6b65\u679a\u4e3e\uff0c\u5e76\u628aCancellationToken\u5e94\u7528\u5230\u5176\u4f7f\u7528\u7684\u679a\u4e3e\u5668\u4e2d\u3002\u5728\u5bf9\u53ef\u679a\u4e3e\u5bf9\u8c61\u5f00\u542f\u65b0\u7684\u679a\u4e3e\u65f6\uff0c\u56e0\u4e3a\u9700\u8981\u4f7f\u7528\u53d6\u6d88\u4ee4\u724c\uff0c\u6240\u4ee5\u5982\u6b64\u5e94\u7528CancellationToken\u662f\u5408\u7406\u7684\u3002\u5728\u67d0\u4e9b\u573a\u666f\uff0c\u5e94\u8be5\u628a\u4e0d\u540c\u7684\u53d6\u6d88\u4ee4\u724c\u4f20\u5165\u9488\u5bf9\u53ef\u679a\u4e3e\u5bf9\u8c61\u7684\u4e0d\u540c\u679a\u4e3e\u4e2d\u3002<br \/>\n\u7b80\u800c\u8a00\u4e4b\uff0c\u53ef\u53d6\u6d88\u7684\u5e76\u975e\u53ef\u679a\u4e3e\u5bf9\u8c61\uff0c\u800c\u662f\u7531\u53ef\u679a\u4e3e\u5bf9\u8c61\u521b\u5efa\u7684\u679a\u4e3e\u5668\u3002\u5f02\u6b65\u6d41\u652f\u6301WithCancellation\u6269\u5c55\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task ConsumeSequence(IAsyncEnumerable&lt;int&gt; items)\n{\n    using var cts=new CancellationTokenSource(500);\n    CancellationToken token=cts.Token;\n    await foreach(int result in items.WithCancellation(token))\n    {\n        Console.WriteLine(result);\n    }\n}\nasync IAsyncEnumerable&lt;int&gt; SlowRange([EnumeratorCancellation] CancellationToken token=defualt)\n{\n    for(int i=0;i!=10;i++)\n    {\n        await Task.Delay(i*100,token);\n        yield return i;\n    }\n}\nawait ConsumeSequence(slowRange());\n\n<\/code><\/pre>\n<p>\u7531\u4e8e\u5b58\u5728EnumeratorCancellation\u53c2\u6570\u5c5e\u6027\uff0c\u56e0\u6b64\u7f16\u8bd1\u5668\u4f1a\u8d1f\u8d23\u5c06\u4ee4\u724c\u4eceWithCancellation\u4f20\u9012\u5230\u7531EnumeratorCancellation\u6807\u8bb0\u7684token\u53c2\u6570\uff0c\u6b64\u65f6\u53d6\u6d88\u8bf7\u6c42\u4f1a\u5728\u5904\u7406\u5b8c\u524d\u51e0\u9879\u4e4b\u540e\uff0c\u8ba9await foreach\u629b\u51faOperationCanceledException\u3002<br \/>\nWithCancellation\u6269\u5c55\u65b9\u6cd5\u4e0d\u4f1a\u963b\u788dConfigureAwait(false),\u53ef\u4ee5\u5c06\u8fd9\u4e24\u79cd\u6269\u5c55\u65b9\u6cd5\u4e32\u8054\u5728\u4e00\u8d77\u3002<\/p>\n<pre><code class=\"language-CSharp line-numbers\">async Task ConsumeSequence(IAsyncEnumerable&lt;int&gt; items)\n{\n    using var cts=new CancellationTokenSource(500);\n    CancellationToken token=cts.Token;\n    await foreach(int result in items.WithCancellation(token).ConfigureAwait(false))\n    {\n        Console.WriteLine(result);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u95ee\u98982 \u89e3\u51b3\u65b9\u68483 \u8ba8\u8bba \u95ee\u9898 \u5047\u8bbe\u9700\u8981\u53d6\u6d88\u5f02\u6b65\u6d41\uff0c\u5e94\u8be5\u600e\u4e48\u505a\u5462\uff1f \u89e3\u51b3\u65b9\u6848 \u5e76\u975e\u6240\u6709\u7684\u5f02 [&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-833","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\/833","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=833"}],"version-history":[{"count":1,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/833\/revisions"}],"predecessor-version":[{"id":834,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/833\/revisions\/834"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}