﻿{"id":1421,"date":"2025-09-13T15:52:02","date_gmt":"2025-09-13T07:52:02","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1421"},"modified":"2025-09-13T15:52:02","modified_gmt":"2025-09-13T07:52:02","slug":"phpbb-107","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1421","title":{"rendered":"CancellationTokenSource\u4e0eTaskCompletionSource\u7684\u533a\u522b\u4e0e\u7528\u6cd5"},"content":{"rendered":"<p><strong class=\"text-strong\">CancellationTokenSource \u548c TaskCompletionSource \u662f .NET \u4e2d\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u7684\u4e24\u4e2a\u6838\u5fc3\u7c7b\uff0c\u5b83\u4eec\u5206\u522b\u89e3\u51b3 \u201c\u5982\u4f55\u53d6\u6d88\u4efb\u52a1\u201d \u548c \u201c\u5982\u4f55\u624b\u52a8\u63a7\u5236 Task \u7684\u5b8c\u6210\u201d \u7684\u95ee\u9898\u3002<\/strong><\/p>\n<p>\u4e00\u3001CancellationTokenSource\t\u63d0\u4f9b\u53d6\u6d88\u4fe1\u53f7\uff0c\u7528\u4e8e\u901a\u77e5\u5f02\u6b65\u64cd\u4f5c\u201c\u4f60\u8be5\u505c\u4e86\u201d\u3002<\/p>\n<ul>\n<li>\u4f5c\u7528\uff1a\u751f\u6210\u4e00\u4e2a\u53d6\u6d88\u4ee4\u724c\uff08CancellationToken\uff09\uff0c\u5e76\u63a7\u5236\u662f\u5426\u53d1\u51fa\u53d6\u6d88\u8bf7\u6c42\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u4f7f\u7528\u573a\u666f\uff1a\u4f60\u60f3\u5728\u5f02\u6b65\u64cd\u4f5c\u4e2d\u652f\u6301\u201c\u4e2d\u9014\u53d6\u6d88\u201d\u3002<\/li>\n<\/ul>\n<ul>\n<li>\u5173\u952e\u6210\u5458\uff1a<\/li>\n<\/ul>\n<p>1. Token\uff1a\u7ed9\u5f02\u6b65\u65b9\u6cd5\u4f20\u5165\u8fd9\u4e2a token\uff0c\u8ba9\u5b83\u80fd\u54cd\u5e94\u53d6\u6d88\u3002<br \/>\n2. Cancel()\uff1a\u89e6\u53d1\u53d6\u6d88\u4fe1\u53f7\u3002<br \/>\n3. CancelAfter(TimeSpan)\uff1a\u5ef6\u8fdf\u53d6\u6d88\u3002<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"csharp\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">Console<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;1.\u751f\u6210\u4e00\u4e2a\u53d6\u6d88\u4ee4\u724c\uff08CancellationToken\uff09\uff0c\u5e76\u5728 5 \u79d2\u949f\u540e\u53d1\u51fa\u53d6\u6d88\u8bf7\u6c42\u3002&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #0600FF; font-weight: bold;\">var<\/span> cts <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> CancellationTokenSource<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">cts<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">CancelAfter<\/span><span style=\"color: #008000;\">&#40;<\/span>TimeSpan<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">FromSeconds<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #FF0000;\">5<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">Console<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;2.\u8c03\u7528\u5f02\u6b65\u4efb\u52a1 DoWorkAsync &#8230;&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">await DoWorkAsync<span style=\"color: #008000;\">&#40;<\/span>cts<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Token<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">Console<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;3.\u7a0b\u5e8f\u7ed3\u675f&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">async Task DoWorkAsync<span style=\"color: #008000;\">&#40;<\/span>CancellationToken token<span style=\"color: #008000;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #008000;\">&#123;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #6666cc; font-weight: bold;\">int<\/span> i <span style=\"color: #008000;\">=<\/span> <span style=\"color: #FF0000;\">0<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #0600FF; font-weight: bold;\">while<\/span> <span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">!<\/span>token<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">IsCancellationRequested<\/span><span style=\"color: #008000;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #008000;\">&#123;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; await Task<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Delay<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #FF0000;\">1000<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; Console<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span>$<span style=\"color: #666666;\">&quot; &nbsp; &nbsp;\u7b2c{++i}\u79d2. Working&#8230;&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #008000;\">&#125;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; Console<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot; &nbsp; &nbsp;\u4efb\u52a1\u88ab\u53d6\u6d88!&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #008000;\">&#125;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"bash\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #000000;\">1<\/span>.\u751f\u6210\u4e00\u4e2a\u53d6\u6d88\u4ee4\u724c\uff08CancellationToken\uff09\uff0c\u5e76\u5728 <span style=\"color: #000000;\">5<\/span> \u79d2\u949f\u540e\u53d1\u51fa\u53d6\u6d88\u8bf7\u6c42\u3002<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #000000;\">2<\/span>.\u8c03\u7528\u5f02\u6b65\u4efb\u52a1 DoWorkAsync &#8230;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; \u7b2c<span style=\"color: #000000;\">1<\/span>\u79d2. Working&#8230;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; \u7b2c<span style=\"color: #000000;\">2<\/span>\u79d2. Working&#8230;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; \u7b2c<span style=\"color: #000000;\">3<\/span>\u79d2. Working&#8230;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; \u7b2c<span style=\"color: #000000;\">4<\/span>\u79d2. Working&#8230;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; \u7b2c<span style=\"color: #000000;\">5<\/span>\u79d2. Working&#8230;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; \u4efb\u52a1\u88ab\u53d6\u6d88<span style=\"color: #000000; font-weight: bold;\">!<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #000000;\">3<\/span>.\u7a0b\u5e8f\u7ed3\u675f<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=107\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CancellationTokenSource \u548c TaskCompletion &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1421\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1421","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1421"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1421\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}