<?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 APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx</link><description>The Asynchronous Programming Model (APM) in the .NET Framework has been around since .NET 1.0 and is the most common pattern for asynchrony in the Framework.&amp;#160; Even if you’re not familiar with the name, you’re likely familiar with the core of the</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#10193776</link><pubDate>Mon, 08 Aug 2011 19:15:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10193776</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Thanks for the doc suggestion, Fritz.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10193776" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#10193737</link><pubDate>Mon, 08 Aug 2011 17:10:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10193737</guid><dc:creator>Fritz</dc:creator><description>&lt;p&gt;What the documentation never states is that FromAsync will also throw whatever exception the BeginOperation method that is passed as an argument might throw. E.g. if you are passing as an argument a .Net APM operation that creates a network connection and the endpoint is not found you will get an exception right then and there when you call the FromAsync method and the task will not be created. This would merit a mention somewhere on msdn, but I couldn&amp;#39;t find anything.&lt;/p&gt;
&lt;p&gt;I was trying to catch exceptions and return a faulted task (so that I only have to do error handling in one place) but that doesn&amp;#39;t work:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Task&amp;lt;Stream&amp;gt; requestTask = Task&amp;lt;Stream&amp;gt;.Factory.FromAsync(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(stream, callback, state) =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return serviceProxy.BeginRequest(stream, callback, state);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp;catch (Exception ex) &amp;nbsp; &amp;nbsp;// e.g. CommunicationException when endpoint can&amp;#39;t be reached&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TaskCompletionSource&amp;lt;Stream&amp;gt; tcs = new TaskCompletionSource&amp;lt;Stream&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tcs.SetException(ex);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return tcs.Task;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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;},&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(asyncResult) =&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp;return serviceProxy.EndRequest(asyncResult);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}, ...);&lt;/p&gt;
&lt;p&gt;The task returned in the case of an exception from BeginRequest is Waiting for Activation, not faulted. It looks like I have to wrap the call to FromAsync in a try/catch block. Any ideas?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10193737" width="1" height="1"&gt;</description></item><item><title>Tasks and the Event-based Asynchronous Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9791859</link><pubDate>Fri, 19 Jun 2009 21:43:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9791859</guid><dc:creator>Parallel Programming with .NET</dc:creator><description>&lt;p&gt;As has been discussed previously, one of the new features in the Task Parallel Library is TaskCompletionSource&amp;amp;lt;TResult&amp;amp;gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9791859" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9771044</link><pubDate>Thu, 18 Jun 2009 02:36:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9771044</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Thanks for your thoughts on this, James.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9771044" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9769830</link><pubDate>Wed, 17 Jun 2009 19:55:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9769830</guid><dc:creator>James B</dc:creator><description>&lt;p&gt;Scenarios... Hmm. Sure thing. Suppose you have a server with some methods that can return from immediately, and some that require file access, database access, etc.&lt;/p&gt;
&lt;p&gt;Now, in Twisted Python, there's a function called defer.maybeDeferred that will take a normal value or a Deferred (essentially a Future&amp;lt;T&amp;gt; in your case), and if it's a normal value call defer.succeed on the value to make it a Deferred. Since .NET is strongly typed this is not necessary. Instead the server would pass back a Future&amp;lt;T&amp;gt;.&lt;/p&gt;
&lt;p&gt;Then, the method calling the server's request function will receive the Future&amp;lt;T&amp;gt; back, and attach callbacks that take in the response and encode it. The encoder returns a Future&amp;lt;T&amp;gt;. The I/O level then attaches callbacks to send it to the other side. Each level modifies the return value.&lt;/p&gt;
&lt;p&gt;Anyway, what the .succeed/.fail feature does is allow this whole process, in the event that there's nothing to wait on, to occur without any switching or scheduling at all. .callback (.NET .ContinueWith) would execute immediately, since the Future&amp;lt;T&amp;gt; returned is already finished. This is a rather common case.&lt;/p&gt;
&lt;p&gt;And yeah, right now I have it inside a FutureHelper I wrote (using the CTP, there is no Factory, so nowhere to put an extension method).&lt;/p&gt;
&lt;p&gt;By the way, have you considered talking to the CLR team? They need to make it possible to use T as void in generics. This would save you an enormous amount of duplication, and could be implemented as an empty struct plus optimizations (i.e. return a; -&amp;gt; return; if a is void, b = a -&amp;gt; no op if b and a are void, etc.). The Action/Func duality is entirely an artifact of this lack, and it's propagating into your API as well. Just a thought :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9769830" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9769788</link><pubDate>Wed, 17 Jun 2009 19:26:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9769788</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Hi James-&lt;/p&gt;
&lt;p&gt;Thanks for the suggestion. &amp;nbsp;We've considered adding such a helper, e.g. Task.Factory.FromResult(value) and Task.Factory.FromException(exc), and may do so at some point in the future. &amp;nbsp;Could you elaborate on the scenarios where this would be useful to you?&lt;/p&gt;
&lt;p&gt;You can also write these yourself as extension methods (in fact, you'll find such extension methods in the Beta 1 samples at &lt;a rel="nofollow" target="_new" href="http://code.msdn.microsoft.com/ParExtSamples"&gt;http://code.msdn.microsoft.com/ParExtSamples&lt;/a&gt;), e.g.&lt;/p&gt;
&lt;p&gt;public static Task&amp;lt;TResult&amp;gt; FromResult&amp;lt;TResult&amp;gt;(this TaskFactory factory, TResult result)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var tcs = new TaskCompletionSource&amp;lt;TResult&amp;gt;();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;tcs.SetResult(result);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return tcs.Task;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9769788" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9769770</link><pubDate>Wed, 17 Jun 2009 19:14:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9769770</guid><dc:creator>James B</dc:creator><description>&lt;p&gt;Hmm... in addition to Begin and End async methods, it might be nice if you added helpers for synchronous methods.&lt;/p&gt;
&lt;p&gt;Twisted Python does something like this, and it's very handy. Oftentimes you have a method that could be asynchronous, but is synchronous in some instances. In those cases, you'd (with TPL) end up doing&lt;/p&gt;
&lt;p&gt;var f = Future&amp;lt;blah&amp;gt;.Create();&lt;/p&gt;
&lt;p&gt;f.Value = value;&lt;/p&gt;
&lt;p&gt;return f;&lt;/p&gt;
&lt;p&gt;It'd be nice if there were a helper, such that I could just return Future&amp;lt;blah&amp;gt;.Succeed(value); or in the case of setting Exception, Future&amp;lt;blah&amp;gt;.Fail(value);&lt;/p&gt;
&lt;p&gt;Look at Twisted Python's Deferred for more inspiration. :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9769770" width="1" height="1"&gt;</description></item><item><title>Tasks and the APM Pattern | VishwaTech IT News</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9769230</link><pubDate>Wed, 17 Jun 2009 15:21:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9769230</guid><dc:creator>Tasks and the APM Pattern | VishwaTech IT News</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://www.vishwatech.com/globalnews/?p=1714"&gt;http://www.vishwatech.com/globalnews/?p=1714&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9769230" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9726663</link><pubDate>Thu, 11 Jun 2009 17:32:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9726663</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Hi Olivier- Thank you for your feedback.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9726663" width="1" height="1"&gt;</description></item><item><title>re: Tasks and the APM Pattern</title><link>http://blogs.msdn.com/b/pfxteam/archive/2009/06/09/9716439.aspx#9726486</link><pubDate>Thu, 11 Jun 2009 15:46:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9726486</guid><dc:creator>StoneFactory</dc:creator><description>&lt;p&gt;Hi Stephen,&lt;/p&gt;
&lt;p&gt;First, I like &amp;nbsp;self descriptive names and I really think FromAsync miss the already started aspect (Task.Wrap and Task.WrapAndStart or something like this should be better).&lt;/p&gt;
&lt;p&gt;Second, I always separates object creation and execution start. It's not same stuff.&lt;/p&gt;
&lt;p&gt;Third, I work on a StateMachine engine where state activities must be described during initialization of state machine. Tasks can be used to implement states activities but Start process must be done only on state entry during execution step (long away from initialization).&lt;/p&gt;
&lt;p&gt;In fact, the initial problem is having your Task implements IAsyncResult. When you start a standard BeginXyz APC, you only get IAsyncResult implementation instance when process have already been started. IAsyncResult is a kind of token describing &amp;nbsp;a currently running asynchronious execution. But Task can be started after construction. So, during time time between construction and Start, your Task is not an IAsyncResult.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9726486" width="1" height="1"&gt;</description></item></channel></rss>