I have a list of objects, each of which has a bool ShouldRun () method.
I am currently repeating a list of objects and checking ShouldRun () on each object and calling Run () on the first to return true
foreach (child in Children) { if (child.ShouldRun()) { child.Run(); break; } }
I would like to do this in parallel because the shouldRun evaluation can take a decent amount of time, and it would be helpful if the later elements in the collection would start an early evaluation.
However, I cannot think of a way to do this to satisfy these conditions:
1 Run only one item
2 Do not start a later item if the previous item is true or has not yet completed the evaluation
3 If all the “earlier” elements returned false and the middle element returned true, do not wait for the next element to complete the evaluation, because you know that it cannot redefine anything before.
I thought about doing a parallel “where” linq query to retrieve all the elements that should be Run () and then sort, but that would violate condition # 3
Ideas?
Background Information:
The system is intended for a generalized AI robotics system.
Some of the tasks with a higher priority can be launched immediately by the known sensor variables, for example: Im crashes, corrects!
other tasks can be computationally intensive (do camera image recognition and achieve visible goals)
other tasks can be databases or remotely managed (find a list of likely object locations from the database, and then go there to see if you can fall into the visible range of one of them)
Some of the tasks themselves have child tasks, which, in essence, will start this process recursively in a subset of the tasks, and the grandson's task will be transferred through the chain
c # foreach parallel-processing linq
Jason coyne
source share