How to invoke an event within Parallel.ForEachAsync in C# without hanging UI

274 Views Asked by At

I'm trying to invoke an event within a Parallel.ForEachAsync, and it's hanging my UI or erroring back to the program.cs

The event on my class is being consumed by the UI to increment a progress bar. It works great when done in a foreach but not in the Parallel.ForEachAsync. I've tried to make my event async but that did not help any.

await Parallel.ForEachAsync<Individual>(INDlist, async (individual, token) =>
    {
        if (Cancel) throw new OperationCanceledException("Interface cancelled by the user");
        await UpdateIndividualAsync(individual);
        await IncrementProgress?.Invoke(this, EventArgs.Empty);
    });
1

There are 1 best solutions below

0
Mark Grecco On BEST ANSWER

The issue was solved by firing the event less often. As the event was consumed by the UI it seemed to overwhelm it