简体   繁体   中英

Run async await tasks in parallel in Asp.net Core

List<Employee> employeeList = new List<Employee>();
List<Event> eventList = new List<Event>();

//Wanted to execute each foreach call asynchronously, to improve performance
foreach (var account in accounts)
{
    var context = await GetContext(account); 
    
    //following two await is depends on above result
    var employees = await _employeeService.List(context);
    var events = await _eventService.List(context).ConfigureAwait(false);
    //Question is how to execute above two await calls parallely
    employeeList.AddRange(employees);
    eventList.AddRange(events);
}

I want to fire off two long-running tasks(employees and Events) at the same time. So maybe can we achieve parallel execution for each account above to solve this?

If you want only each call inside loop to be in parallel -

List<Employee> employeeList = new List<Employee>();
List<Event> eventList = new List<Event>();
foreach (var account in accounts)
{
    var context = await GetContext(account);
    var employeesTask = _employeeService.List(context);
    var eventsTask = _eventService.List(context);
    await Task.WhenAll(employeesTask , events).ConfigureAwait(false);
    var employees = await employeesTask.ConfigureAwait(false);
    var events= await eventsTask.ConfigureAwait(false);    
    employeeList.AddRange(employees);
    eventList.AddRange(events);
}

You could just schedule execution of each iteration as TAsk and put it array and then use Task.WhenAll , like below:

var tasks = new List<Task>();
//Wanted to execute each foreach call asynchronously, to improve performance
foreach (var account in new int[] { 1 })
{
    tasks.Add(
        Task.Run(async () =>
        {
            var context = await GetContext(account);

            //following two await is depends on above result
            var employees = await _employeeService.List(context);
            var events = await _eventService.List(context).ConfigureAwait(false);
            //Question is how to execute above two await calls parallely
            employeeList.AddRange(employees);
            eventList.AddRange(events);
        }));
}
await Task.WhenAll(tasks);

WARNING: you have to make sure that employeeList and eventList are thread safe.

You could even wrap loop iteration in a separate method (you can define itinside your method) to make code cleaner:

await Task.WhenAll(accounts.Select(a => loopIteration(a)));

async Task loopIteration(Account account)
{
    var context = await GetContext(account);

    //following two await is depends on above result
    var employees = await _employeeService.List(context);
    var events = await _eventService.List(context).ConfigureAwait(false);
    //Question is how to execute above two await calls parallely
    employeeList.AddRange(employees);
    eventList.AddRange(events);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM