I have a long Task job that uses callbacks to send data step by step (rather than a single ContinueWith () callback at the end).
I want to be able to pass the Task object back to this callback for the purpose of identifying the task (using Task.CurrentId)
However, I cannot decide how to pass the Task object to the task delegate. There seems to be no overload for this, and I cannot use closure to do this, since the task object is not defined at this point.
eg.
public Task StartDoingSomeStuff(CallbackDelegate callback) { Task task = Task.Factory.StartNew(() => { while(whatever) { var results = DoSomeStuff(); callback(results, task); //CS0165. How do I get hold of the task? } }); return task; }
gives:
error CS0165: use of an unassigned local variable 'task'
source share