<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx</link><description>One of the great features that crosses all of Parallel Extensions types is a consistent approach to cancellation (see http://blogs.msdn.com/pfxteam/archive/2009/05/22/9635790.aspx ). In this post we explore some of the ways cancellation is used in Parallel</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Interesting Finds: June 23, 2009</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9799223</link><pubDate>Tue, 23 Jun 2009 14:18:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9799223</guid><dc:creator>Jason Haley</dc:creator><description>&lt;p&gt;Interesting Finds: June 23, 2009&lt;/p&gt;
</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9803440</link><pubDate>Thu, 25 Jun 2009 14:56:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9803440</guid><dc:creator>Pter</dc:creator><description>&lt;p&gt;I see that you delete comments that you dislike. If you don't want comments then don't put this pretty form on your blog!!!&lt;/p&gt;
</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9803772</link><pubDate>Thu, 25 Jun 2009 18:46:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9803772</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;Pter, we don't delete comments (unless they're obscene), and we value any and all feedback, whether positive or critical. &amp;nbsp;Are you referring to a comment that you made that is no longer showing up? &amp;nbsp;I see you commented on another post we have on cancellation, at &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/pfxteam/archive/2009/05/22/9635790.aspx"&gt;http://blogs.msdn.com/pfxteam/archive/2009/05/22/9635790.aspx&lt;/a&gt;... is it possible you thought you posted on this post rather than on that one?&lt;/p&gt;
</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9804703</link><pubDate>Fri, 26 Jun 2009 06:33:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9804703</guid><dc:creator>Joe Albahari</dc:creator><description>&lt;p&gt;Hi Stephen&lt;/p&gt;
&lt;p&gt;Tell me, where is that ThrowIfCancellationRequested() method defined? Is it an extension method?&lt;/p&gt;
&lt;p&gt;It would certainly make sense to define such a method on CancellationToken (I actually went *looking* for something like this the first time I used CancellationToken!)&lt;/p&gt;
&lt;p&gt;Joe&lt;/p&gt;
</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9805738</link><pubDate>Fri, 26 Jun 2009 20:57:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805738</guid><dc:creator>mikelid</dc:creator><description>&lt;p&gt;Sorry for the confusion..and thanks for bringing it to our attention.&lt;/p&gt;
&lt;p&gt;Unfortunately that method slipped through and was not included in Beta1, but it will ship in the next release. &lt;/p&gt;
&lt;p&gt;In the meantime, you can use your own extension method to acheive the same behavior that we will ship.&lt;/p&gt;
&lt;p&gt;public static class CancellationTokenHelpers{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;public static void ThrowIfCancellationRequested(this CancellationToken ct){&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; if(ct.IsCancellationRequested)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new OperationCanceledException(&amp;quot;The operation was canceled.&amp;quot;, ct);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;-Mike.&lt;/p&gt;
</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9856108</link><pubDate>Mon, 03 Aug 2009 13:36:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9856108</guid><dc:creator>Zhou</dc:creator><description>&lt;p&gt;I'm trying to understand the Cancellation Framework machinery. I know that it is important to cancel operation in a cooperative manner. I've understood the CancellationTaskSource concept. But I've problems to understand the behavior with Task.WaitAll(...). I have the following example:&lt;/p&gt;
&lt;p&gt;public void Cancellation()&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;CancellationTokenSource cts = new CancellationTokenSource();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Task t1 = new Task(() =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while (true)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (Task.Current.CancellationToken.IsCancellationRequested)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Canceled&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Task is running.&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Thread.Sleep(1000);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;});&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Task t2 = new Task(() =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while (true)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (Task.Current.CancellationToken.IsCancellationRequested)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Canceled&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Task is running.&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Thread.Sleep(1000);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;});&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;t1.Start();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;t2.Start();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;new Task(() =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Thread.Sleep(1000);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cts.Cancel();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}, TaskCreationOptions.None).Start();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Task.WaitAll(new Task[] { t1, t2}, cts.Token);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I assumed that the Cancellation request is propagated to the waiting Task (in this example t1, t2) by the Cancellation Framework. But in my example the defined OperationCanceledException is thrown. In this case it is not possible to manage the cancellation request within task1 and task2. Like this behavior it is possible that the running tasks own shared resources (Deadlock) or shared state will be invalid after cancellation. &lt;/p&gt;
</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9856413</link><pubDate>Mon, 03 Aug 2009 21:04:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9856413</guid><dc:creator>mikelid</dc:creator><description>&lt;P&gt;The issue here is that you are canceling two distinct types of operation: you are asking for cancellation of the tasks, and you are also asking for cancellation of the wait operation itself. &amp;nbsp;Sometimes this may be appropriate, but most often you will want to genuinely wait until the tasks have completed (with the understanding that the tasks may complete by observing a request for cancellation).&lt;/P&gt;
&lt;P&gt;Furthermore, because waiting for task completion is the common approach to marshal any exceptions the tasks may have suffered, allowing the Wait() operation itself to be canceled is preventing the marshalling from occuring and may be counterproductive.&lt;/P&gt;
&lt;P&gt;ie, use Task.WaitAll(tasks) rather than Task.WaitAll(tasks, ct).&lt;/P&gt;
&lt;P&gt;-mike.&lt;/P&gt;</description></item><item><title>re: Cancellation in Parallel Extensions</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/22/9791840.aspx#9856890</link><pubDate>Tue, 04 Aug 2009 11:07:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9856890</guid><dc:creator>Zhou</dc:creator><description>&lt;p&gt;Thanks Mike for your comment. &lt;/p&gt;
&lt;p&gt;I thought I did a mistake and used the new API in a wrong way ... Ok, I understand that the CancelToken for the WaitXXX-Operation is only responsible to cancel the waiting state like a timeout. &lt;/p&gt;
&lt;p&gt;But it is a break regarding the CancellationFramework (CF) and the framework don't meet the requirements for a cancellation-&amp;quot;aware&amp;quot; framework. Maybe in a future API version the given CancelToken is transfered to all participants. &lt;/p&gt;
</description></item></channel></rss>