<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Richard&#039;s Random Ramblings &#187; Parallel Extensions</title>
	<atom:link href="http://blog.rjcox.co.uk/category/dev/parallel-extensions/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rjcox.co.uk</link>
	<description>Whatever I think is interesting... mostly about computing but not always</description>
	<lastBuildDate>Tue, 22 Jun 2010 13:29:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Parallel Framework Extensions: Three Minor Things I Didn&#8217;t Know This Morning</title>
		<link>http://blog.rjcox.co.uk/2010/02/08/pfx_three_things/</link>
		<comments>http://blog.rjcox.co.uk/2010/02/08/pfx_three_things/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 23:23:12 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Parallel Extensions]]></category>

		<guid isPermaLink="false">http://blog.rjcox.co.uk/?p=89</guid>
		<description><![CDATA[1. You Can Add a Continuation To A Completed Task

  And the continuation will be run. Given the code:

var t1 = Task.Factory.StartNew(() =&#62; { /* Noop */ });
Thread.Sleep(100); // Ensure t1 has run
Trace.Assert(t1.Status == TaskStatus.RanToCompletion);
// Now t1 is completed... we can still add a continuation:
bool continued = false;
var t2 = t1.ContinueWith(t =&#62; { continued [...]]]></description>
			<content:encoded><![CDATA[<h3>1. You Can Add a Continuation To A Completed Task</h3>
<p>
  And the continuation will be run. Given the code:
</p>
<pre>var t1 = Task.Factory.StartNew(() =&gt; { /* Noop */ });
Thread.Sleep(100); // Ensure t1 has run
Trace.Assert(t1.Status == TaskStatus.RanToCompletion);
// Now t1 is completed... we can still add a continuation:
bool continued = false;
var t2 = t1.ContinueWith(t =&gt; { continued = true; });
t2.Wait();
Trace.Assert(continued);</pre>
<p>
  Neither assert will fire. Thus the continue block has executed despite
  being added to Task <code>t1</code> <i>after</i> <code>t1</code> had run.
  This does of course prevent a race between <code>TaskFactory.StartNew</code>
  returning a task that is scheduled (and therefore possibly already completed)
  with a call to <code>Task.ContinueWith</code>.
</p>

<h3>2. You Can Continue A Task Multiple Times</h3>
<p>
  And the continuations run concurrently. I&rsquo;m not sure that this would
  be useful very often (only if multiple tasks need to be started following a
  single initial task), but nice to know there is not an arbitrary zero or one
  multiplicity on continuations. Sample code:
</p>
<pre>var t1 = new Task(() =&gt; { Console.WriteLine("  This is the original task"); });
var t2 = t1.ContinueWith(t =&gt; {
    Console.WriteLine("   This is the first continiation (before sleep: thread #{0})", Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(250);
    Console.WriteLine("   This is the first continiation (after sleep: thread #{0})", Thread.CurrentThread.ManagedThreadId);
});
var t3 = t1.ContinueWith(t =&gt; {
    Console.WriteLine("   This is the second continiation (before sleep: thread #{0})", Thread.CurrentThread.ManagedThreadId);
    Thread.Sleep(250);
    Console.WriteLine("   This is the second continiation (after sleep: thread #{0})", Thread.CurrentThread.ManagedThreadId);
});
t1.Start();
Task.WaitAll(new[] { t1, t2, t3 });</pre>
<p>
  Depending on the system this can give different results, but one should see the two
  &ldquo;before sleep&rdquo; messages before either of the two &ldquo;after sleep&rdquo;
  messages: both tasks are executing concurrently.
</p>

<h3>3. <code>TaskFactory.FromAsync</code> Starts the Task</h3>
<p>
  No illustration of this, it just doesn&rsquo;t say so in the documentation.
  I discovered this when a <code>InvalidOperationException</code> was thrown for
  starting an already complete task (only two statements later, the intermediate
  one adding a continuation). To be fair to the designers, there is really no
  reason to want to delay the execution of the task as the creation is a single
  statement (and if you really needed to, just use a helper (anonymous) function).
</p>]]></content:encoded>
			<wfw:commentRss>http://blog.rjcox.co.uk/2010/02/08/pfx_three_things/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

