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 child methods have completed.
I was trying to get my head around the async/await
syntax but I just don't get it.
Here's the code:
public PewPew SomeMethod(Foo foo)
{
var cats = GetAllTheCats(foo);
var food = GetAllTheFood(foo);
return new PewPew
{
Cats = cats,
Food = food
};
}
private IList<Cat> GetAllTheCats(Foo foo)
{
// Do stuff, like hit the Db, spin around, dance, jump, etc...
// It all takes some time.
return cats;
}
private IList<Food> GetAllTheFood(Foo foo)
{
// Do more stuff, like hit the Db, nom nom noms...
// It all takes some time.
return food;
}
So with that code above, I want to say : go and get all the cats and food at the same time. Once we're finished, then return a new PewPew
.
I'm confused because I'm not sure which classes above are async
or return a Task
, etc. All of em? just the two private ones? I'm also guessing I need to leverage the Task.WaitAll(tasks)
method, but I'm unsure how to setup the tasks to run at the same time.
Suggestions, kind folks?
Foo foo
will be shared between threads. Make sure you lock correctly. – Allegory