<?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>Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx</link><description>As has been discussed previously, one of the new features in the Task Parallel Library is TaskCompletionSource&amp;lt;TResult&amp;gt; , which enables the creation of a Task&amp;lt;TResult&amp;gt; that represents any other asynchronous operation. There are a wide variety</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9792452</link><pubDate>Sat, 20 Jun 2009 03:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792452</guid><dc:creator>Barry Kelly</dc:creator><description>&lt;p&gt;You neatly demonstrate all the horribleness of the EAP style, when contrasted with the easier to use APM.&lt;/p&gt;
&lt;p&gt;I must ask, though, how do you unsubscribe from the *Completed events using your helpers?&lt;/p&gt;
&lt;p&gt;I was most frustrated when EAP turned up in .NET 2.0; EAP was chosen ahead of APM for web-service proxies, IIRC. APM was used in one of the earlier CTPs, and was a joy to use, but then EAP went into the RTM version and broke all my code. Demultiplexing stateful events invoked upon completion of stateless async method calls? Yuck.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9792459</link><pubDate>Sat, 20 Jun 2009 03:36:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9792459</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;Hi Barry-&lt;/p&gt;
&lt;p&gt;Thanks for the comments. &amp;nbsp;Regarding unsubscribing, the implementations in the Beta 1 samples (and the last example shown in the blog post itself) unregister from the *Completed event automatically when the operation completes. &amp;nbsp;Are you looking to unregister prior to the operation completing? &amp;nbsp;If so, there are a variety of solutions. &amp;nbsp;One would be, prior to returning from the helper, to register a handler with the Task's CancellationToken such that when the Task has cancellation requested, it unregisters from the *Completed event and transitions the Task into the Canceled state.&lt;/p&gt;
&lt;p&gt;I hope that helps.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9793825</link><pubDate>Sat, 20 Jun 2009 18:12:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9793825</guid><dc:creator>Aleksander Polak</dc:creator><description>&lt;p&gt;This caught my attention:&lt;/p&gt;
&lt;p&gt;if (e.Cancelled) tcs.TrySetCanceled();&lt;/p&gt;
&lt;p&gt;Although English is not my native language, I believe the form &amp;quot;Canceled&amp;quot; should be used in both cases. (As far as I remember this was even mentioned in &amp;quot;Framework Design Guidelines&amp;quot;.) At the very least the naming should be consistent. Yes, both forms can be found in the BCL today, but not within a single API.&lt;/p&gt;
&lt;p&gt;Any plans to fix that before you ship the bits?&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9794010</link><pubDate>Sun, 21 Jun 2009 01:36:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9794010</guid><dc:creator>Mike</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;I have the following Problem.&lt;/p&gt;
&lt;p&gt;I have a few Validation Tasks. All of them get their Data from a &amp;quot;Linq to XML&amp;quot; Source. I am using &amp;quot;AsParrallel&amp;quot; for this Linq Ressource.&lt;/p&gt;
&lt;p&gt;Now i want to run all these Task parrallel and i am interested only in the following:&lt;/p&gt;
&lt;p&gt;- If all of this Taskes finish successful then move on.&lt;/p&gt;
&lt;p&gt;- If one of this Tasks throw an Exception then immediatelly kill all other tasks and give back the exception of this task which has thrown it.&lt;/p&gt;
&lt;p&gt;Is this Doable with the new Task Library.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9794491</link><pubDate>Sun, 21 Jun 2009 07:20:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9794491</guid><dc:creator>MichaelGG</dc:creator><description>&lt;p&gt;I don't know if I'll ever understand why MS added the silly event async pattern :(. With APM we can easily define a generic wrapper for all sorts of methods, without needing detailed wiring up code. &lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9795857</link><pubDate>Sun, 21 Jun 2009 20:31:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9795857</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;Aleksander-&lt;/p&gt;
&lt;p&gt;You're correct that Canceled (one L) is the right spelling to be used, per the latest Framework guidelines, and it's why the Task Parallel Library uses that spelling. &amp;nbsp;The &amp;quot;e.Cancelled&amp;quot; you're seeing is not coming from TPL, it's coming from the AsyncCompletedEventArgs class which shipped as part of the .NET Framework 2.0 to support the Event-based Asynchronous Pattern (EAP). &amp;nbsp;This post is simply showing how to take classes that implement the EAP pattern using that API, and get them to interoperate with Tasks.&lt;/p&gt;
&lt;p&gt;Thanks for the suggestion, though.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9795878</link><pubDate>Sun, 21 Jun 2009 20:37:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9795878</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;Mike, you can certainly do this with tasks, taking advantage of ContinueWhenAll, ContinueWhenAny, and cancellation. &amp;nbsp;You might also be able to achieve the same thing by extending your PLINQ solution to first iterate through all of the XML files, since PLINQ will try to stop executing a query as fast as possible when an exception is thrown from elsewhere in the query. &lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9795884</link><pubDate>Sun, 21 Jun 2009 20:39:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9795884</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;MichaelGG, thanks for the comment.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9808371</link><pubDate>Mon, 29 Jun 2009 12:19:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9808371</guid><dc:creator>Joe Albahari</dc:creator><description>&lt;p&gt;What exactly was the design goal of the EAP? Was it simply to achieve parallel execution - or was it intended as a wrapper around APM?&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9808646</link><pubDate>Mon, 29 Jun 2009 17:44:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9808646</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;EAP was designed to simplify one-off asynchronous operations that are initiated from and need to conclude on the UI thread (or, more generally, in some SynchronizationContext). &amp;nbsp;With a type like WebClient, in a client app you can asynchronously download some data and handle the downloaded data on the UI thread. &amp;nbsp;Or in ASP.NET, you can implement an async page that uses some asynchronously downloaded data, and again achieve that relatively easily using a type like WebClient that implements the EAP pattern. &amp;nbsp;The pattern starts to lose steam, however, when you need to compose multiple asynchronous operations, or when you don't know at async operation launch time what you want to do with the result of the operation when it's complete, etc. &amp;nbsp;In short, it can be quite useful for some scenarios, but puts up roadblocks for other more complicated ones.&lt;/p&gt;
&lt;p&gt;In any event, the purpose of this blog post was to demonstrate how Tasks can be used to represent existing EAP implementations, just as Tasks can be used to represent existing APM implements.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9854270</link><pubDate>Fri, 31 Jul 2009 12:26:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9854270</guid><dc:creator>Charles Prakash Dasari</dc:creator><description>&lt;p&gt;Though EAP was introduced for one-off async operations like you mentioned above, MSDN documentation goes far and beyond to recommend EAP over APM. I find APM more convenient than EAP particularly for the following reasons:&lt;/p&gt;
&lt;p&gt;1) Exception handling&lt;/p&gt;
&lt;p&gt;2) Explicit call to EndXXXX method&lt;/p&gt;
&lt;p&gt;3) No multiple listeners business (in EAP you could have multiple listeners for the completion event. While this may be an interesting side-affect, it is still a potential candidate for bugs especially when the effect is not foreseen by the developer of the EAP functions than the one who consumes).&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9935022</link><pubDate>Thu, 10 Dec 2009 08:47:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9935022</guid><dc:creator>holatom</dc:creator><description>&lt;p&gt;I belive that there is a bug in the example above, in lines (final version of DownloadDataTask):&lt;/p&gt;
&lt;p&gt;TransferCompletionToTask(tcs, e, () =&amp;gt; e.Result); &lt;/p&gt;
&lt;p&gt;webClient.DownloadDataCompleted -= handler; &lt;/p&gt;
&lt;p&gt;because second line is executed even if &lt;/p&gt;
&lt;p&gt; if (e.UserState == tcs) &lt;/p&gt;
&lt;p&gt;(in TransferCompletionToTask) is false, that causes handler unregistration prior &amp;quot;right&amp;quot; DownloadDataCompleted is raised.&lt;/p&gt;
</description></item><item><title>re: Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/pfxteam/archive/2009/06/19/9791857.aspx#9935214</link><pubDate>Thu, 10 Dec 2009 17:10:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9935214</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;Great catch, holatom. &amp;nbsp;You're correct. &amp;nbsp;I'll fix the issue.&lt;/p&gt;
</description></item></channel></rss>