How can I pass a state object to a continuation task?

You can't. They expect you to use the power of closures. Just define an additional variable inside the loops to capture the current value for that closure. See this answer from Jon Skeet for more details on capturing and closures.

Update: Or Jon Skeet could beat me to reply directly to your question saying exactly the same thing. :)


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.


For future readers, ContinueWith() function, at least in .NET 4.7, has an overload that allows you to pass state parameter:

public System.Threading.Tasks.Task ContinueWith(
  Action<System.Threading.Tasks.Task,object> continuationAction, object state,
  System.Threading.CancellationToken cancellationToken, 
  System.Threading.Tasks.TaskContinuationOptions continuationOptions, 
  System.Threading.Tasks.TaskScheduler scheduler);

See this MSDN article for more details.