<?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>Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx</link><description>Imagine that you have a Task handed to you by a third party, and that you would like to force this Task to complete within a specified time period. However, you cannot alter the “natural” completion path and completion state of the Task, as that may cause</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414907</link><pubDate>Mon, 29 Apr 2013 21:34:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414907</guid><dc:creator>Joe Hoag</dc:creator><description>&lt;p&gt;@thxmike: The ultimate disposition of taskA is the concern of the person who creates/launches taskA. &amp;nbsp;The purpose of Task.TimeoutAfter is to return a *proxy* (taskB in the example above) for taskA that will either (1) be completed with a TimeoutException if taskA fails to complete within the specified time span, or (2) be completed with taskA&amp;#39;s completion information if taskA completes within the specified time span.&lt;/p&gt;
&lt;p&gt;This proxy can be used to effectively &amp;quot;time out&amp;quot; a task that was handed to you, which you did not create, without actually perturbing that original task. &amp;nbsp;Consider something like this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Task taskA; // handed to you&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;var proxy = taskA.TimeoutAfter(1000); // times out after one second&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;proxy.ContinueWith( antecedent =&amp;gt; {DoSomething(antecedent);});&lt;/p&gt;
&lt;p&gt;That continuation can now fire, one way or another, whether or not taskA completes. &amp;nbsp;The DoSomething() method can check to see whether or not &amp;quot;antecedent&amp;quot; completed or timed out, and act accordingly.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414907" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414901</link><pubDate>Mon, 29 Apr 2013 21:13:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414901</guid><dc:creator>thxmike</dc:creator><description>&lt;p&gt;@Joe Hoag: I get it now. Very Interesting. &amp;nbsp;Thanks for you patience. This next question may be out of scope but what happens to the orphaned taskA, lets say it never stops executing ?(i.e. some run away code, or some type of blocking code which never lets it complete).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414901" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414876</link><pubDate>Mon, 29 Apr 2013 19:55:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414876</guid><dc:creator>Joe Hoag</dc:creator><description>&lt;p&gt;@thxmike: The ContinueWith will only be called if the original task (the &amp;quot;task&amp;quot; parameter to TimeoutAfter) completes. &amp;nbsp;When the timer expires, it will cause the task *returned* by TimeoutAfter to complete. &amp;nbsp;So:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;Task taskA = Task.Factory.StartNew(...);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;taskB = taskA.TimeoutAfter(1000); // 1 second&lt;/p&gt;
&lt;p&gt;If the timeout expires, taskB will complete with a TimeoutException. &amp;nbsp;taskA may well continue running.&lt;/p&gt;
&lt;p&gt;Does that help?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414876" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414872</link><pubDate>Mon, 29 Apr 2013 19:37:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414872</guid><dc:creator>thxmike</dc:creator><description>&lt;p&gt;@Joe Hoag: I am attempting to use the ContinueWith indicated in the first post with .Net 4. The execution gets to the timers timeout, sets the exception, &amp;nbsp;but then never calls ContinueWith after. I think I am just missing something but not sure what.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414872" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414833</link><pubDate>Mon, 29 Apr 2013 17:07:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414833</guid><dc:creator>Joe Hoag</dc:creator><description>&lt;p&gt;@thxmike: Can you simply revert to the ContinueWith that was used in the first implementation given in the post? &amp;nbsp;That should work on .NET4.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414833" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414807</link><pubDate>Mon, 29 Apr 2013 15:56:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414807</guid><dc:creator>thxmike</dc:creator><description>&lt;p&gt;After some research I found that this is a .net 4.5 framework feature. Do we know if this can work in a 4.0 framework way. So far my attempts have been unsuccessful. The ContinueWith is never called. Any assistance you can provide would be helpful. Thank you.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414807" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414777</link><pubDate>Mon, 29 Apr 2013 14:54:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414777</guid><dc:creator>Joe Hoag</dc:creator><description>&lt;p&gt;@Michael Rivera: Are you using .NET4 or .NET4.5? &amp;nbsp;I believe that the overload in question only exists in the latter.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414777" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10414615</link><pubDate>Sun, 28 Apr 2013 19:53:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414615</guid><dc:creator>Michael Rivera</dc:creator><description>&lt;p&gt;I attempted to implement this code, however, I found that the ContinueWith line errors with the following:&lt;/p&gt;
&lt;p&gt;&amp;quot;No overload for method &amp;#39;ContinueWith&amp;#39; takes 5 arguments&amp;quot;. Is there something else that needs to be added or changed to get this to work.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414615" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10236910</link><pubDate>Mon, 14 Nov 2011 18:26:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236910</guid><dc:creator>Omer Mor</dc:creator><description>&lt;p&gt;You are right of course. Don&amp;#39;t know how I&amp;#39;ve missed it. In that case - I&amp;#39;d go with the Rx option that Steffen mentioned. It&amp;#39;s concise, readable, and I love Rx :-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236910" width="1" height="1"&gt;</description></item><item><title>re: Crafting a Task.TimeoutAfter Method</title><link>http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235834.aspx#10236852</link><pubDate>Mon, 14 Nov 2011 16:45:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10236852</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Omer, while that will provide expected behavior for the returned task, it will also block a thread for the duration of the timeout. &amp;nbsp;In contrast, the solutions provided in this post will not block threads unnecessarily. &amp;nbsp;Especially for timer-related functionality, this becomes very important, as it&amp;#39;s reasonable to expect a process to have thousands of these active at a time.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10236852" width="1" height="1"&gt;</description></item></channel></rss>