I'm trying to implement a nested task inside a loop - this is the pattern I have so far, however I'm far from sure as this is the first time I've used the parallel task library.
The parent (tier) task should wait for the children (node) tasks to complete.
public int NestedTask(IEnumerable<MatchTier> tierNodes)
{
var tier = Task<int>.Factory.StartNew(() =>
{
Task<int> node = null;
foreach(var n in tierNodes)
{
node = Task<int>.Factory.StartNew(() =>
{
// Task logic goes here
return 1; // temp placeholder
});
// if a valid value is returned then exit this loop
}
return node.Result;
});
return tier.Result;
}
The child nodes loop until the first valid value is returned, then the loop should be exited, passing the valid value to the parent.
Both the child and parent nodes require a timeout too. Each child will be allowed approx 3 seconds to run after which the process will be timed out and the next node interrogated.
the parent has an overall timeout value of approx 15 - 20 seconds, after which, if no valid response has been recieved, it too should terminate.
Does this seem logical?