Parallel.For - What's the Best Way to Show Progress?

Parallel.For - What's the Best Way to Show Progress?

I have a time consuming task which the new Parallel.For routine is ideally suited to. In a single thread environment I show progress using a ProgressBar which is updated inside the loop.

I thought the parallel version would be a doddle - but it's not.

If I update the ProgressBar using a TThread.Synchronize wrapper it becomes deadlocked. The main thread is waiting for the For loop to end, and the For loop need the main thread to perform the synchronized code.  This is something which was highlighted in a recent Coderage video.

The second option is to update the code using a TThread.Queue wrapper. This works! However, it seems the progress updates all come once the main loop has completed (since the cores are busy doing the For loop).

The third option is to call the Parallel.For loop as a TTask and use synchronize inside the loop.  This way the main loop isn't waiting for the For loop to end. This also works and the progress bar looks nice. However, the routine exits before the for loop has completed i.e. before the long update has finished calculating. This means, the application could be unstable.

So, my question is, what is the best practice way of providing feedback to the user when processing a long Parallel For routine?

Comments