This is not behaving quite as I thought it would the need is simple, launch a number of tasks to do operations on an object. One unique object per task. The second part is a ContinueWith when each task reports the results. However, I am not getting a WhenAll type behavior. Hopefully someone can set me straight.
_tasks = new Task<AnalysisResultArgs>[_beansList.Count];
for (int loopCnt = 0; loopCnt < _beansList.Count; loopCnt++)
{
_tasks[loopCnt] = Task<AnalysisResultArgs>.Factory.StartNew(() =>
{
return _beansList[loopCnt].Analyze(newBeanData);
});
await _tasks[loopCnt].ContinueWith(ReportResults,
TaskContinuationOptions.RunContinuationsAsynchronously)
// do some housekeeping when all tasks are complete
}
private void ReportResults(Task<AnalysisResultArgs> task)
{
/* Do some serial operations
}
It was my understanding that _beansList.Count tasks would be launched and by using await on the ContinueWith the housekeeping work won't execute until all the Tasks have completed. I cannot block, as I need to be sure to be able to throttle the incoming data to prevent way too many tasks waiting to be executed.
Where did I goof, the await actually completes and the housekeeping gets run even though not ALL of the tasks have run to completion.
Task.Runinstead ofStartNew, andawaitinstead ofContinueWith, then your code will be cleaner and behave better.StarNewis dangerous and whyContinueWithis dangerous.