Cannot update multiple progressbars asynchronously
This works well:
This doesn't work well:
Simpler examples:
What makes the output a bit uncertain, after applying .Sleep() it is correct:
Enumerable .Range(0, 10) .ToList() .ForEach(x => { var progressBar = new Util.ProgressBar().Dump(); for (var i = 0; i <= 64; i++) { Thread.Sleep(10); progressBar.Fraction = (double)i / 64.0; } });I see 10 progressbars updating sequentially from 0% to 100% (OK).
This doesn't work well:
ParallelEnumerable .Range(0, 10) .ForAll(x => { var progressBar = new Util.ProgressBar().Dump(); for (var i = 0; i <= 64; i++) { Thread.Sleep(10); progressBar.Fraction = (double)i / 64.0; } });I'd expect to see 10 progress bars updating simultaneously, however I only see one progressbar updating from 0% to 100% (NOK).
Simpler examples:
var pb1 = new Util.ProgressBar().Dump(); var pb2 = new Util.ProgressBar().Dump(); pb2.Fraction = 1;Result: Two progress bars are visible, one 0% and one 100% (OK)
var pb1 = new Util.ProgressBar().Dump(); pb1.Fraction = 1; var pb2 = new Util.ProgressBar().Dump();Result: One progress bar is visible at 100% (NOK)
What makes the output a bit uncertain, after applying .Sleep() it is correct:
var pb1 = new Util.ProgressBar().Dump(); pb1.Fraction = 1; Thread.Sleep(100); var pb2 = new Util.ProgressBar().Dump(); pb2.Fraction = .5;Result: Two progress bars are visible, one at 100% and one at 50% (OK).
Comments
Also bear in mind that ParallelEnumerable is not designed for I/O-bound work and you might not get the desired level of concurrency. Try this instead: Or with Reactive Extensions: