0

Here is my code:

List<customerModel> rowResults = DataFromCustomersTable();

ParallelLoopResult ploopResult = Parallel.ForEach(rowResults, po, async row =>
    {
        // Check if customer has a default profile
       ResponseResultModel response = await CustomerDefaultSourceTest(row);
       // Update table with results from above test
       string message = await custData.UpdateResult(row, response);
    });
// Loop completed

Both the methods - CustomerDefaultSourceTest and UpdateResult have try/catch block.

I removed the try catch from inside the Parallel.ForEach loop and still only one top row keeps getting processed.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
vambat
  • 1
  • 2
  • For async, you'll be wanting the [async version.](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreachasync?view=net-7.0) – StuartLC Nov 16 '22 at 20:16
  • 1
    The problem is that the `Parallel.ForEach` does not understand async delegates. So it launches a bunch of [`async void`](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void) operations and then immediately completes. And then most likely your program also completes, so nothing really happens. The solution is to switch to the [`Parallel.ForEachAsync`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreachasync) (.NET 6). – Theodor Zoulias Nov 16 '22 at 20:19

0 Answers0