Nesting await in Parallel.ForEach

后端 未结 9 1319
别跟我提以往
别跟我提以往 2020-11-22 01:01

In a metro app, I need to execute a number of WCF calls. There are a significant number of calls to be made, so I need to do them in a parallel loop. The problem is that th

9条回答
  •  长情又很酷
    2020-11-22 01:45

    You can save effort with the new AsyncEnumerator NuGet Package, which didn't exist 4 years ago when the question was originally posted. It allows you to control the degree of parallelism:

    using System.Collections.Async;
    ...
    
    await ids.ParallelForEachAsync(async i =>
    {
        ICustomerRepo repo = new CustomerRepo();
        var cust = await repo.GetCustomer(i);
        customers.Add(cust);
    },
    maxDegreeOfParallelism: 10);
    

    Disclaimer: I'm the author of the AsyncEnumerator library, which is open source and licensed under MIT, and I'm posting this message just to help the community.

提交回复
热议问题