How can I wait till the Parallel.ForEach completes

You don’t have to do anything special, Parallel.Foreach() will wait until all its branched tasks are complete. From the calling thread you can treat it as a single synchronous statement and for instance wrap it inside a try/catch.

Update:

The old Parallel class methods are not a good fit for async (Task based) programming. But starting with dotnet 6 we can use Parallel.ForEachAsync()

await Parallel.ForEachAsync(items, (item, cancellationToken) =>
  {
     await ... 
  });

There are a few overloads available and the ‘body’ method should return a ValueTask.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)