title: How can I pass a state object to a continuation task?
body: <p>I'm using the .NET 4.0 Task Parallel Library with C# (my first time using TPL)</p>
  
  <p>I have a task A which I want to run to completion before firing off a bunch of other tasks (B,C,D, etc). I therefore want to create tasks B,C,D etc as continuations of task A. 
  However, I want to pass a 'state' object to task B, another state object to task C, etc.</p>
  
  <p>I can pass a state object to task A by simply using a Task constructor overload that takes a state object, for example <a href="http://msdn.microsoft.com/en-us/library/dd783035.aspx">http://msdn.microsoft.com/en-us/library/dd783035.aspx</a> describes this Task constructor overload:</p>
  
  <pre><code>Task(Action&lt;Object&gt;, Object, CancellationToken) 
  </code></pre>
  
  <p>This works fine, and the second argument is my 'state' object.</p>
  
  <p>I want to create a continuation task, e.g. for task B:</p>
  
  <pre><code>Task taskB = taskA.ContinueWith(/* args here*/)
  </code></pre>
  
  <p>However, I cannot see a ContinueWith() overload (see <a href="http://msdn.microsoft.com/en-us/library/dd235663.aspx">http://msdn.microsoft.com/en-us/library/dd235663.aspx</a>) which allows me to pass a 'state' object to a continuation task. How can this be done?</p>
  
  <p>Notes: </p>
  
  <ol>
  <li>I do not have the 'state' object for taskB available at the time I create taskA</li>
  <li>The 'state' object for taskB is not an output (return value) of taskA</li>
  </ol>
  
  <p>For some context, what I am doing is creating taskB, taskC, etc. inside a couple of loops and so I am passing the value of the loop variables to taskB, taskC, etc. using a state object, in order to avoid the problem of always ending up with the final value of the loop variables in the tasks (the closure issue).</p>