<?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>Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx</link><description>Parallel Extensions makes it easy to wait for Tasks to complete. Task exposes a Wait method, which can be used trivially: Task t = Task.Create(...); ... t.Wait(); Task also exposes several static methods for waiting on an array of tasks, either for all</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Pregnant Man &amp;raquo; Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8836234</link><pubDate>Wed, 06 Aug 2008 09:22:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8836234</guid><dc:creator>Pregnant Man &amp;raquo; Waiting for Tasks</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://wordnew.acne-reveiw.info/?p=16361"&gt;http://wordnew.acne-reveiw.info/?p=16361&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8836666</link><pubDate>Wed, 06 Aug 2008 11:51:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8836666</guid><dc:creator>Marshall Brooke</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You could also extend the code to follow through the waitable time period of the CountdownEvent&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public bool Wait()&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;return Wait(TimeSpan.MinValue);&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;public bool Wait(TimeSpan wait)&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;bool ret = false;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!_doneAdding) &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;_doneAdding = true;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_ce.Decrement(); &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;ret = _ce.Wait(wait);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!ret)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ret;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (!_faulted.IsEmpty) &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Task.WaitAll(_faulted.ToArray());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return true;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8837782</link><pubDate>Wed, 06 Aug 2008 18:21:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8837782</guid><dc:creator>toub</dc:creator><description>&lt;P&gt;Thanks, Marshall; certainly something you could add. Although you wouldn't want to use TimeSpan.MinValue like this: -1 is actually the sentinel value for an infinite timeout. &amp;nbsp;You probably want to make the overload "public bool Wait(int millisecondsTimeout)" and then pass in Timeout.Infinite or -1 as the timeout value used by the parameterless Wait overload.&lt;/P&gt;</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8838061</link><pubDate>Wed, 06 Aug 2008 19:40:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8838061</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;I assume what Marshall wanted is to wait for the minimum amount of time, which would be this:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return Wait(TimeSpan.Zero);&lt;/p&gt;
&lt;p&gt;Using TimeSpan.MinValue in that way will actually throw an ArgumentOutOfRangeException.&lt;/p&gt;
&lt;p&gt;You could wait infinitely like this:&lt;/p&gt;
&lt;p&gt;task.Wait(new TimeSpan(0,0,0,0,-1));&lt;/p&gt;
&lt;p&gt;But, I think that's unintuitive and contradictory. &amp;nbsp;I think it would be much clearer if Task.Wait(TimeSpan) threw and ArgumentOutOfRangeException on any &amp;quot;negative&amp;quot; TimeSpan (i.e. where ticks is less than 0, not -1). &amp;nbsp;The sentinel value for &amp;quot;infinite&amp;quot;, with Wait(TimeSpan) should then be TimeSpan.MaxValue.&lt;/p&gt;
&lt;p&gt;The fact that the underlying framework uses -1 for infinity is a leaky abstraction with Wait(TimeSpan).&lt;/p&gt;
&lt;p&gt;I would suggest Task.Wait(TimeSpan timeout) be written more like:&lt;/p&gt;
&lt;p&gt;	long milliseconds = -1;&lt;/p&gt;
&lt;p&gt;	if (timeout != TimeSpan.MaxValue)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		if (timeout.Ticks &amp;lt; 0 || timeout.TotalMilliseconds &amp;gt; Int32.MaxValue)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			throw new ArgumentOutOfRangeException(&amp;quot;timeout&amp;quot;);&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;		milliseconds = (long)timeout.TotalMilliseconds;&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	return this.Wait((int)milliseconds);&lt;/p&gt;
</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8840912</link><pubDate>Thu, 07 Aug 2008 14:19:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8840912</guid><dc:creator>Marshall Brooke</dc:creator><description>&lt;p&gt;Yes, I caught that issue with Timespan, seems weird to me that Minvalue is not actually the minimum value, but actually means something else. &amp;nbsp;Timespan.Infinite would be more logical, but hey ho. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;The reason I needed to add the Timeout was that you can never guarantee that a task will actually ever return (infinite loop etc) and this would break long running apps. &amp;nbsp;Perhaps the Task.Create could include a Timeout overload that is a reasonable period of time for the task to complete before firing an exception?&lt;/p&gt;
&lt;p&gt;By the way guys, I was using the CCR before and took a lot of persuading to move over to TPL as I really liked the Port methodology. &amp;nbsp;CCR however is licensed at $399 and I'm really liking the TPL API at the moment. &amp;nbsp;One issue I had with the CCR was the Policy Constraints you could place on a port for scheduling which were giving me some problems where the Pool was hung in cases, but this was resolved with the TPL by creating different TaskManagers with different number of threads - really nice :)&lt;/p&gt;
&lt;p&gt;I am now fully converted to TPL, but with the abstract nature of the library, the posts in this blog are essential to understaning how to use it.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Marshall&lt;/p&gt;
</description></item><item><title>ParallelWhileNotEmpty</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8841929</link><pubDate>Fri, 08 Aug 2008 02:41:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8841929</guid><dc:creator>Parallel Programming with .NET</dc:creator><description>&lt;p&gt;Parallel Extensions includes the System.Threading.Parallel class, which provides several high-level loop&lt;/p&gt;
</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#8843719</link><pubDate>Fri, 08 Aug 2008 18:55:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8843719</guid><dc:creator>Peter Ritchie</dc:creator><description>&lt;p&gt;@Marshal. &amp;nbsp;TimeSpan.MinValue is the minium value; but as with signed types it's a negative value.&lt;/p&gt;
&lt;p&gt;In many of the contexts that TimeSpan is used, a negative span of time is meaningless, unfortunately.&lt;/p&gt;
</description></item><item><title> Parallel Programming with NET Waiting for Tasks | Best Eye Cream</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#9706983</link><pubDate>Mon, 08 Jun 2009 05:41:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9706983</guid><dc:creator> Parallel Programming with NET Waiting for Tasks | Best Eye Cream</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://besteyecreamsite.info/story.php?id=642"&gt;http://besteyecreamsite.info/story.php?id=642&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#9847792</link><pubDate>Fri, 24 Jul 2009 21:50:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9847792</guid><dc:creator>Tamer Yousef</dc:creator><description>&lt;p&gt;are there similar examples for the new .net 4.0? some of the core methods have been changed, also make sure you include visual basic 10 examples, not everyone can translate what you have from C# to vb.net.&lt;/p&gt;
&lt;p&gt;-thanks.&lt;/p&gt;
</description></item><item><title>re: Waiting for Tasks</title><link>http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx#9859729</link><pubDate>Fri, 07 Aug 2009 03:37:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9859729</guid><dc:creator>toub</dc:creator><description>&lt;p&gt;Hi Tamer-&lt;/p&gt;
&lt;p&gt;This functionality and more is certainly possible with .NET 4, albeit with slightly changed syntax. &amp;nbsp;We'll definitely follow-up with such examples, hopefully in the near future.&lt;/p&gt;
&lt;p&gt;Thanks for the interest! &amp;nbsp;Requests like this help us to prioritize.&lt;/p&gt;
</description></item></channel></rss>