Код: Выделить всё
private async Task PopulateValuesFromCacheToDictionaryTasks(
List searchResults)
{
List taskList = new();
foreach (var result in searchResults)
{
var currentId = result.Id;
var parentTask = _appCache.GetAsync(currentId ).ContinueWith(
x =>
{
if (_appDictionary.TryAdd(currentId , x.Result))
{
_logger.LogInformation($"Successfully added entry for {currentId } in dictionary");
}
else
{
_logger.LogError($"Unable to add entry {currentId } in dictionary");
}
}, TaskContinuationOptions.OnlyOnRanToCompletion);
taskList .Add(parentTask);
}
return taskList ;
}
//In the calling code
var listOfCacheFetchTasks =
PopulateValuesFromCacheToDictionaryTasks(searchResults);
await Task.WhenAll(listOfCacheFetchTasks );
// further processing based on the dictionary added values
Пытался добавить родительскую и дочернюю задачу по отдельности и ожидал обе задачи. тоже не работает
Подробнее здесь: https://stackoverflow.com/questions/782 ... th-c-sharp