Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Timer to measure Parallel.ForEach progress pauses unexpectedly

I'm using Parallel.ForEach for the first time, where I'm processing files; in the context of a WinForms app.

As per guidance from other threads on this coding problem (Parallel.ForEach progress reporting) I have a public (int) counter property on my class which contains the parallel code, and it's successfully updating; I also have a Timer on my Form that periodically reads the counter.

The issue is that when I execute the parallel code the visible progress updating appears to stop, and then starts as soon as the parallel code is complete.

FYI - I'm calling the parallel code directly - that is, not through a background worker or async method.

like image 772
Adrian K Avatar asked May 05 '26 18:05

Adrian K


1 Answers

Parallel.ForEach actually evaluates the query in parallel fashion but does wait for finishing of execution and blocks the calling thread.

You should use it on a separate thread/backgroundworker/task to get your progress variable updating while not blocking the UI.

like image 103
SimpleVar Avatar answered May 09 '26 03:05

SimpleVar