Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to await multiple tasks, that were created inside await foreach?

When using AsAsyncEnumerable and await foreach, each item is lazy loaded. Per item I want to call a method DoSomething, that returns Task. The method calls should happen in parallel. When all tasks complete, the below method should return Unit.

public async Task<Unit> Handle(SomeRequest request, CancellationToken cancellationToken)
{
    var entities = _dbContext.MyModel
        .Where(...)
        .AsAsyncEnumerable();

    await foreach (var item in entities)
    {
        _ = item.DoSomething();
    }

    return Unit.Value;
}

I am not sure how to await all tasks when using await foreach. Would this be the right approach?:

public async Task<Unit> Handle(SomeRequest request, CancellationToken cancellationToken)
{
    var entities = _dbContext.MyModel
        .Where(...)
        .AsAsyncEnumerable();

    List<Task> tasks = new();

    await foreach (var item in entities)
    {
        tasks.Add(item.DoSomething());
    }

    await Task.WhenAll(tasks);

    return Unit.Value;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The given example should work, especially if you expect fairly few items.

Since you have no (visible) mechanism to limit the number of concurrent tasks you might suffer problems if you have a lots of items. You might for example exhaust the threadpool. If the amount of work done per item is very small it might also benefit from processing batches of items. To handle cases like these you might consider using something like DataFlow, it should allow you to specify the degree of parallelism.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading