{"id":56065,"date":"2010-11-04T13:53:00","date_gmt":"2010-11-04T13:53:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2010\/11\/04\/new-feature-delayed-cancellation\/"},"modified":"2010-11-04T13:53:00","modified_gmt":"2010-11-04T13:53:00","slug":"new-feature-delayed-cancellation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/new-feature-delayed-cancellation\/","title":{"rendered":"New Feature? :: Delayed cancellation"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p class=\"MsoNormal\">We&rsquo;re interested in adding support for scheduling\ncancellation.&nbsp; For example:<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\">\/\/ Create a\ntoken source that will Cancel() after a delay<br>\nvar cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\"><br><\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\">\/\/ And\/or\nschedule a Cancel() call<br>\ncts.CancelAfter(TimeSpan.FromMilliseconds(100));<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\"><br><\/span><\/p>\n<p class=\"MsoNormal\">We&rsquo;ve heard from many folks that this is a desired feature\nand have found good uses for it ourselves.&nbsp;\nInterestingly, we would also allow CancelAfter() to be called multiple\ntimes to reset the delay (provided the token source was not already canceled,\nof course).&nbsp; One could also effectively\ncancel the cancellation by calling CancelAfter() with TimeSpan.MaxValue.<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">One potential issue we&rsquo;ve been thinking through is whether\/how\nto deal with exceptions that fly out of user-registered delegates.&nbsp; Recall that we strongly discourage throwing\nexceptions from callbacks registered with a CancellationToken, though it is\npossible to handle such exceptions today:<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\">cts.Token.Register(()\n=&gt; { throw new Exception(&ldquo;HA!&rdquo;); });<\/p>\n<p>try { cts.Cancel(); } \/\/ invokes the callbacks<br>\ncatch { \/\/ Caught it! }<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: 'courier new', courier\"><br><\/span><\/p>\n<p class=\"MsoNormal\">However, if you use this new feature to cancel\nasynchronously, you won&rsquo;t ever have a chance to handle the exception, and it\nwould likely bring down your application.&nbsp;\nSome are of the opinion that we need not do anything about this, but\nwe&rsquo;ve been brainstorming potential solutions anyway.&nbsp; We&rsquo;re definitely curious what you think\n=).&nbsp; Feel free to answer these questions\nif you want:<\/p>\n<ol>\n<li>What do you think about unhandled exceptions\nflying out of user-created token callbacks?&nbsp;\nShould we support this use case?<\/li>\n<li>Any feedback about the general feature?<\/li>\n<\/ol>\n<p class=\"MsoNormal\">Thanks!<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; We&rsquo;re interested in adding support for scheduling cancellation.&nbsp; For example: &nbsp; \/\/ Create a token source that will Cancel() after a delay var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); \/\/ And\/or schedule a Cancel() call cts.CancelAfter(TimeSpan.FromMilliseconds(100)); We&rsquo;ve heard from many folks that this is a desired feature and have found good uses for it ourselves.&nbsp; Interestingly, [&hellip;]<\/p>\n","protected":false},"author":485,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7908],"tags":[7907,7915,7921,7927,7909],"class_list":["post-56065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-net-4","tag-cancellation","tag-feedback-requested","tag-new-feature","tag-parallel-extensions"],"acf":[],"blog_post_summary":"<p>&nbsp; We&rsquo;re interested in adding support for scheduling cancellation.&nbsp; For example: &nbsp; \/\/ Create a token source that will Cancel() after a delay var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); \/\/ And\/or schedule a Cancel() call cts.CancelAfter(TimeSpan.FromMilliseconds(100)); We&rsquo;ve heard from many folks that this is a desired feature and have found good uses for it ourselves.&nbsp; Interestingly, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/56065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/485"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=56065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/56065\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=56065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=56065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=56065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}