Nested task inside loop
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
});
}
return node.Result;
});
return tier.Result;
}
Does this seem logical?
No comments:
Post a Comment