Xerxes Xerxes - 1 year ago 76
C# Question

Am I getting the benefits of async and await in the following code

I have the following code in which I am using an async keyword in my GroupBy clause, however I am told by intellisence that this call is not awaited and will run synchronously, my question is would my two calls to

and my subsequent call to
Task.WaitAll(languagesTask, storesTask);
use async features or would all my code run synchronously since I am not awaiting the GroupBy statement. also is it possible to await the
.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
.Execute(new AllStoresCriteria());

Answer Source

The basic rule of thumb is "Without an await, async will run synchronously".

You could easily get your GroupBy() to run properly async by exchanging Task.WaitAll() for await Task.WhenAll().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download