Completion Action not being called

Jul 5, 2010 at 8:41 AM
Edited Jul 5, 2010 at 8:42 AM
I'm having an issue where the completion action isn't being called.
currentTasks.Add(
    Parallel.StartBackground(
        todoTasks[0],
        delegate
        {
            currentTasks.RemoveAt(0);
        }
    )
);
I keep an eye on the currentTasks collection which is just a simple List<Task> and I can see that the task completes but the completion action/delegate never gets called. EDIT: Oh and the todoTasks is defined like so: List<IWork>
Coordinator
Jul 5, 2010 at 4:39 PM
Parallel.RunCallbacks() needs to be called for the task completion actions to be executed. This system was added so that you could use results calculated by a task back on the main thread, thereby often not having to worry about any synchronization code. For example, adding an item to a game scene, after asynchronously loading the items' model. If just want some code to execute when the task is complete, then the easiest way to do that is to place the code at the end of the tasks' work; either by modifying the work, or by wrapping it in a delegate which executes the work then calls the callback. In your case, you may want to be careful doing that, as the RemoveAt operation will be called by multiple threads, so you will need to lock the collection or use one which can handle concurrent access.