<?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>Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx</link><description>In this post, which is the third one in my parallel programming introduction series, I want to show how you can cancel parallel operations when working with the Task Parallel Library (TPL) . I’m going to modify the program that I started in the previous</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10415856</link><pubDate>Fri, 03 May 2013 10:15:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10415856</guid><dc:creator>RockyLOMO</dc:creator><description>&lt;p&gt;thx&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10415856" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10241652</link><pubDate>Sat, 26 Nov 2011 05:20:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10241652</guid><dc:creator>westseyi</dc:creator><description>&lt;p&gt;Thank you.&lt;/p&gt;
&lt;p&gt;You freed me from all that multithreading, background work , UI locking, Passing Data to a Thread Horror !!!&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10241652" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10237510</link><pubDate>Tue, 15 Nov 2011 23:29:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10237510</guid><dc:creator>Luca</dc:creator><description>&lt;p&gt;@Alexandra Rusina&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt; When i want to make a task to be long-running,&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt; I have to pass the creation option to be long running,&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt; but then the Taskscheduler is required,&lt;/p&gt;
&lt;p&gt;&amp;gt;&amp;gt; then how do i pass the taskscheduler parameter?&lt;/p&gt;
&lt;p&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;gt; You can simply pass null, it should be OK.&lt;/p&gt;
&lt;p&gt;Actually MSDN specifies that an ArgumentNullException &amp;quot;is thrown when the scheduler argument is null&amp;quot; and my tests confirm this.&lt;/p&gt;
&lt;p&gt;You may want to use TaskScheduler.Current instead.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10237510" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10115939</link><pubDate>Fri, 14 Jan 2011 18:27:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10115939</guid><dc:creator>Terry</dc:creator><description>&lt;p&gt;Add a Thread.Sleep into the iteration code with a duration of say 5 seconds. Then 1 second after the loop has began, close the form. The threads can&amp;#39;t exit even though you diligently made such a request in the form_closing event as each thread is locked in the &amp;#39;&amp;#39;pretend long running 3rd party web-service call or library call&amp;#39; that (Thread.Sleep is supposed to represent this). The user thinks the app has closed, but it ain&amp;#39;t. =/&lt;/p&gt;
&lt;p&gt;Further, if the loop logic raises events and those events are subscribed to by the UI, in which the UI perhaps increments a progress-bar and event fired when the form is closed and long gone, the event would execute code that raises exceptions on deallocated form resources. &lt;/p&gt;
&lt;p&gt;Said user is now baffled as an error and an application debug/shut down request dialogue appears from no where.... I think we need a thread abort construct adding to this new library, and the dev should handle resource deallocation etc within the Thread.Abort Exception catch they put in the iteration logic. &lt;/p&gt;
&lt;p&gt;I hope MS don&amp;#39;t expect me to implement my own TaskShedualer to do this.. I think they do.. oh dear time to get the sledge hammer out. =(&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10115939" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10102242</link><pubDate>Wed, 08 Dec 2010 20:29:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10102242</guid><dc:creator>eisenpony</dc:creator><description>&lt;p&gt;I understand the usefulness of throwing to cancel immediately out of a thread, but is using exceptions to control flow of a program considered best practice? It seems to me that if you are writing cooperateive cancellation, you should gracefully end the thread without throwing.&lt;/p&gt;
&lt;p&gt;If you do use the ThrowIfCancellationRequested() method, and need to use it in a try block, how can you avoid catching it? is this acceptable?&lt;/p&gt;
&lt;p&gt;try&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tokenSource.Token.ThrowIfCancellationRequested();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;result += Math.Exp(Math.Log(i) / root);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;catch (System.OperationCanceledException)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;catch (Exception e)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Handle e&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10102242" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10102240</link><pubDate>Wed, 08 Dec 2010 20:19:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10102240</guid><dc:creator>Berryl</dc:creator><description>&lt;p&gt;@Nemo. I found myself adding the Token param also as I went through the code. Definite improvement&lt;/p&gt;
&lt;p&gt;@@codesbad. I agree the initialization should not be done in the declaration by the final code iteration, but hey - it doesn&amp;#39;t sour an otherwise *very* useful offering for this target audience, IMO. In fact I can&amp;#39;t remember the last time I saw some meaty material from MS that was this well produced.&lt;/p&gt;
&lt;p&gt;@Alexandra. Great job, will look for more of the same from you! Cheers&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10102240" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10096416</link><pubDate>Thu, 25 Nov 2010 09:02:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10096416</guid><dc:creator>9rune5</dc:creator><description>&lt;p&gt;Before I can ask my colleagues to use this otherwise excellent article as a point of reference, the code is in need of some cleanup.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;CancellationTokenSource tokenSource = new CancellationTokenSource();&lt;/p&gt;
&lt;p&gt;// ....&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tokenSource = new CancellationTokenSource();&lt;/p&gt;
&lt;p&gt;Any reason why tokenSource is initialized in the declaration? That looks like a leftover from the previous version of this code. It is cleaner to test if tokenSource==null in cancel_Click() instead of making sure the object is always created.&lt;/p&gt;
&lt;p&gt;Ordinarily I am all for &amp;quot;left as an exercise for the reader&amp;quot;, but this is MS&amp;#39;, what, third or fourth attempt at trying to lure regular developers to utilize some sort of threading library. IMO this requires absolute 100% top-notch quality code in all sample code and documentation.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10096416" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10093042</link><pubDate>Thu, 18 Nov 2010 08:21:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10093042</guid><dc:creator>Nemo1571</dc:creator><description>&lt;p&gt;The TokenSource and associated Token are fully thread-safe. &amp;nbsp;You never pass the actual TokenSource to any other thread so the other threads simply call the thread-safe Token::ThrowIfCancellationRequested method directly.&lt;/p&gt;
&lt;p&gt;Except I just noticed this IS using the TokenSource, something like below should be used instead:&lt;/p&gt;
&lt;p&gt;public double SumRootN(int root, Token token)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;double result = 0;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;for (int i = 1; i &amp;lt; 10000000; i++)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;token.ThrowIfCancellationRequested();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;result += Math.Exp(Math.Log(i) / root);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return result;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;.&lt;/p&gt;
&lt;p&gt;var compute = Task.Factory.StartNew(() =&amp;gt;&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return SumRootN(j, tokenSource.Token);&lt;/p&gt;
&lt;p&gt;}, tokenSource.Token);&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10093042" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10051344</link><pubDate>Wed, 18 Aug 2010 07:25:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10051344</guid><dc:creator>Axiom</dc:creator><description>&lt;p&gt;Ok, i was wrong in the second case. My fault. Sorry.&lt;/p&gt;
&lt;p&gt;But i still think that ACCESSING the field is not thread safe.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10051344" width="1" height="1"&gt;</description></item><item><title>re: Parallel Programming: Task Cancellation</title><link>http://blogs.msdn.com/b/csharpfaq/archive/2010/07/19/parallel-programming-task-cancellation.aspx#10051067</link><pubDate>Tue, 17 Aug 2010 16:27:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10051067</guid><dc:creator>Alexandra Rusina</dc:creator><description>&lt;p&gt;@Axiom&lt;/p&gt;
&lt;p&gt;1. This article talks only about cancellation. The TPL handles synchronization and thread-safety itself in this case. &lt;/p&gt;
&lt;p&gt;2. I am not updating a UI thread from background thread. I execute this task directly on the UI thread, this is what the task scheduler is for.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10051067" width="1" height="1"&gt;</description></item></channel></rss>