I have the code below and it just hangs on the Task.WaitAll line. I'm guessing this is due a deadlock where the main thread is waiting on it's synch context to continue execution but this context is also required by one of the continuations in order to finish it's execution as outlined here:
http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
protected override async Task<Person> GetOrders()
{
var person = new Person();
var task1 = GetPersonOrders(person);
var task2 = GetPersonAddresses(person);
var tasks = new List<Task>(){task1, task2}
Task.WaitAll(tasks.ToArray());
}
public async Task GetPersonOrders(Person person)
{
...
person.PersonOrders = await GetPersonOrdersFromRepository();
}
public async Task<List<Order>> GetPersonOrdersFromRepository()
{
...
return await CallSomeWebService;
}
public async Task GetPersonAddresses(Person person)
{
...
person.Addresses = await GetPersonAddressesFromRepository();
}
public async Task<List<Address>> GetPersonAddressesFromRepository()
{
...
return await CallSomeWebService;
}
Questions
1) Why does altering the final of GetOrders() to
await Task.WhenAll(tasks);
fix everything? Why is the deadlock resolved by awaiting Task.WhenAll over Task.WaitAll?
2) Are these tasks still being run in Parallel with the Task.WhenAll modification?