Running async methods in parallel

async-await, c#, task-parallel-library

Solution

Is there a better to run async methods in parallel, or are tasks a good approach?

Yes, the "best" approach is to utilize the `Task.WhenAll` method. However, your second approach should have ran in parallel. I have created a .NET Fiddle, this should help shed some light. Your second approach should actually be running in parallel. My fiddle proves this!

Consider the following:

public Task<Thing[]> GetThingsAsync()
{
    var first = GetExpensiveThingAsync();
    var second = GetExpensiveThingAsync();

    return Task.WhenAll(first, second);
}

Note

It is preferred to use the "Async" suffix, instead of `GetThings` and `GetExpensiveThing` - we should have `GetThingsAsync` and `GetExpensiveThingAsync` respectively - source.

Problem

I've got an async method, `GetExpensiveThing()`, which performs some expensive I/O work. This is how I am using it: ``` // Serial execution public async Task<List<Thing>> GetThings() { var first = await GetExpensiveThing(); var second = await GetExpensiveThing(); return new List<Thing>() { first, second }; } ``` But since it's an expensive method, I want to execute these calls in in parallel. I would have thought moving the awaits would have solved this: ``` // Serial execution public async Task<List<Thing>> GetThings() { var first = GetExpensiveThing(); var second = GetExpensiveThing(); return new List<Thing>() { await first, await second }; } ``` That didn't work, so I wrapped them in some tasks and this works: ``` // Parallel execution public async Task<List<Thing>> GetThings() { var first = Task.Run(() => { return GetExpensiveThing(); }); var second = Task.Run(() => { return GetExpensiveThing(); }); return new List<Thing>() { first.Result, second.Result }; } ``` I even tried playing around with awaits and async in and around the tasks, but it got really confusing and I had no luck. Is there a better to run async methods in parallel, or are tasks a good approach?

Original source

Related problems