I have a long running Task that uses callbacks to feed data incrementally (rather than a single ContinueWith() callback at the end).
I want to be able to pass the Task object back in this callback for task identification purposes (using Task.CurrentId)
However, I cannot work out how to pass the Task object into the task delegate. There appears to be no overload to do this, and I can’t use the closure to do it since the task object is not defined at that 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 unassigned local variable ‘task’
Split declaring the variable and assigning the task to it into two statements. Make sure that you don’t use the variable before the task has been assigned: