I'm using the .NET 4.0 Task Parallel Library with C# (my first time using TPL)
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.
I can pass a state object to task A by simply using a Task constructor overload that takes a state object, for example http://msdn.microsoft.com/en-us/library/dd783035.aspx describes this Task constructor overload:
Task(Action<Object>, Object, CancellationToken)
Task taskB = taskA.ContinueWith(/* args here*/)
The simplest approach would probably be to simply capture it in the
Func<Task, TResult> you pass into
ContinueWith. For example:
object taskBState = GetStateHere(); Task taskB = taskA.ContinueWith(task => RealContinuation(task, taskBState));
Personally I find it easier to capture state like that than getting the state passed in anyway.