다음 코드는 내 GroupBy 절에서 비동기 키워드를 사용하지만 intellisence가이 호출을 기다리지 않고 동 기적으로 실행한다고 말합니다 내 질문은 GetAllLanguages
및 GetAllStores
내 두 호출 및 Task.WaitAll(languagesTask, storesTask);
비동기 기능을 사용하여 후속 호출 또는 GroupBy 문을 기다리고 있지 않으므로 모든 코드를 동 기적으로 실행할 것입니다. .GroupBy(async resourceApplicationType ..
호출을 기다리는 것도 가능합니다.비동기의 장점을 얻고 다음 코드에서 기다리고 있습니다
public async Task UpdateAllResources()
{
var applicationTypes = databaseSettings.Select(dbcs => dbcs.ResourceApplicationType);
var commandsPerApplication = applicationTypes
.GroupBy(async resourceApplicationType =>
{
var languagesTask = GetAllLanguages(resourceApplicationType);
var storesTask = GetAllStores(resourceApplicationType);
var resourceCategory = ResourceApplicationToCategoriesMapper.Map(resourceApplicationType);
Task.WaitAll(languagesTask, storesTask);
var commands = from category in resourceCategory
from language in languagesTask.Result
from store in storesTask.Result
select new UpdateResourcesCommand
{
ApplicationType = resourceApplicationType,
StoreCode = store.Code,
LocaleCode = language.Locale,
ResourceCategory = category
};
return new
{
applicationType = resourceApplicationType, commands
};
});
commandsPerApplication.ForEach(commandsGroup =>
{
commandsGroup.Key.Result.commands.ForEach(async command =>
{
await commandExecutor.Execute<UpdateResourcesCommand, Task>(command);
});
});
}
private Task<IEnumerable<Language>> GetAllLanguages(ResourceApplication applicationType)
{
return allLanguagesQueryRetriever.GetByComponentName(InstanceName.GetUniqueName<IAllLanguagesQuery>(applicationType.ToString()))
.Execute(new AllLanguagesCriteria());
}
private Task<IEnumerable<Store>> GetAllStores(ResourceApplication applicationType)
{
return allStoresQueryRetriever
.GetByComponentName(InstanceName.GetUniqueName<IAllStoresQuery>(applicationType.ToString()))
.Execute(new AllStoresCriteria());
}
당신은'Task.WaitAll (languagesTask, storesTask)를 기다리고하지 않는
UI thread/Synchronization context
블록으로,Task.WaitAll()
로 교착 상태에 바인딩되어'에 'UpdateAllResources' 함수입니다. 그 결과는 당신이 결과에 접근하기 전에 끝나지 않았 음을 의미 할 수 있습니다. – JConstantine@JLevett 그 진술은 기다릴 수 없습니다. "Type void는 기다릴 수 없습니다", 그것은 본질적으로 기다리고 있습니다. – Xerxes
사과드립니다. 'await Task.WhenAll()'을 사용할 수도 있습니다. – JConstantine