I have three tasks A, B, C.
B must run before C, but A can run in parallel to both of them. This is logic that has recently changed, and the logic we currently have works fine, I just wondered if there is a better solution to what we currently have:
public void RunTasks(...) {
Action<IRunnableTask> runner = task =>
{
if (task.ShouldRun(request))
{
task.Run(request, response);
}
};
// run parallel tasks
Parallel.ForEach(parallelTasks, runner);
// run serial tasks
Array.ForEach(serialTasks, runner);
}
Here, A and B would be parallel tasks, C is in the list of serial tasks. The problem is here that the code will wait for A to finish before C can start, which is unnecessary.
So, is there a nice clean solution, or do I need to start putting in callbacks and whatnot?
You could make use of Tasks from the TPL.
You could run Tasks B and A at the same time, then have task C run as a continuation of Task B
e.g.
In the continuation of TaskB, you can pass in the Task to get any result of TaskB passed into the TaskC method.