Nesting await in Parallel.ForEach

后端 未结 9 1314
别跟我提以往
别跟我提以往 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:48

    I am a little late to party but you may want to consider using GetAwaiter.GetResult() to run your async code in sync context but as paralled as below;

     Parallel.ForEach(ids, i =>
    {
        ICustomerRepo repo = new CustomerRepo();
        // Run this in thread which Parallel library occupied.
        var cust = repo.GetCustomer(i).GetAwaiter().GetResult();
        customers.Add(cust);
    });
    

提交回复
热议问题