1

I have the below code with a parallel loop and the parallel loop calls another method Process() and waits on it.

var index = 0;

var options = new ParallelOptions { MaxDegreeOfParallelism = maxParallelCount };

ParallelLoopResult result = Parallel.ForEach(customers, options, async cust =>
    {
        var currentCount = Interlocked.Increment(ref index);

        Console.WriteLine("Processing {0}/{1}", currentCount, totalCount);
       
        var success = await Process(cust);

        if (success)
            Console.WriteLine("({0}/{1}) SUCCESS", cust.Name, cust.Number);
        else
            Console.WriteLine("({0}/{1}) FAILED", cust.Name, cust.Number);
    });

    if (result.IsCompleted)
        Console.WriteLine("process completed | {0} customers processed.", totalCount);

When I run this code, I see the console logs in the following order. Why is the ParallelLoopResult not waiting for the method Process() to finish ? How can I improve this code to wait for the Process() to finish ?

process completed | 10 customers processed.
Customer Name1/Number1 SUCCESS
Customer Name1/Number4 SUCCESS
Customer Name1/Number3 SUCCESS
Customer Name1/Number2 SUCCESS
etc..

Expected order

Customer Name1/Number1 SUCCESS
Customer Name1/Number4 SUCCESS
Customer Name1/Number3 SUCCESS
Customer Name1/Number2 SUCCESS
etc..
process completed | 10 customers processed.
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Jyina
  • 2,530
  • 9
  • 42
  • 80
  • Did you see this?: https://devblogs.microsoft.com/pfxteam/implementing-a-simple-foreachasync/ – GH DevOps May 15 '23 at 11:33
  • 1
    You're parallelly start processing with `Parallel.ForEach`. Either use `Parallel.ForEachAsync` or start all tasks, collect them into list and then wait with `Task.WhenAll`. Use first approach for CPU-intensive tasks and later for IO-bound tasks – JL0PD May 15 '23 at 11:33
  • 2
    If you're using a newer version of c# you can use Parallel.ForeachAsync() – GH DevOps May 15 '23 at 11:34
  • 1
    This may indicate a problem in the design. Parallel loops are most useful for compute bound code, while async/await is most useful for IO-bound code. So, are you certain that you benefit from running multiple async operations in parallel? – JonasH May 15 '23 at 11:40

0 Answers0