I have a function that collects in a Dictionary
values like this:
void Func()
{
....
var dtDict = await HandleComputeBooster();
...
}
async private static Task
DoBooster(..., ConcurrentDict<string, DataTable> dtDict,....)
{
DataTable dt = ...
...
dtDict[symbol] = dt;
...
}
This is the function that is returning too soon:
async private Task<ConcurrentDictionary<string, DataTable>>
HandleComputeBooster()
{
var dtDict = new ConcurrentDictionary<string, DataTable>();
....
var chunks = listOfBoosterSymbols.ChunkBy(8);
var pcCount = Environment.ProcessorCount;
Parallel.ForEach(chunks, new ParallelOptions
{ MaxDegreeOfParallelism = pcCount - 2 }, async listStr =>
{
var symbol = listStr[0];
await DoBooster(..., dtDict, ...);
}
);
...
return dtDict;
}
the problem is that HandleComputeBooster returns before all the values in dtDict are computed. All the values eventually make into dtDict
, but I need a way to say, don't return from HandleComputeBooster until all the chunks have been processed?