I have a method which does 2 independent pieces of logic. I was hoping I can run them both at the same time .. and only continue afterwards when both those
Adding to the other answers, you could do something like:
public PewPew SomeMethod(Foo foo)
{
Task> catsTask = GetAllTheCatsAsync(foo);
Task> foodTask = GetAllTheFoodAsync(foo);
// wait for both tasks to complete
Task.WaitAll(catsTask, foodTask);
return new PewPew
{
Cats = catsTask.Result,
Food = foodTask.Result
};
}
public async Task> GetAllTheCatsAsync(Foo foo)
{
await Task.Delay(7000); // wait for a while
return new List();
}
public async Task> GetAllTheFoodAsync(Foo foo)
{
await Task.Delay(5000); // wait for a while
return new List();
}