Home Task completes before notifying progress. Why and how to fix?
Reply: 0

Task completes before notifying progress. Why and how to fix?

user1880 Published in April 22, 2018, 10:07 pm

So I'm trying to use task progress notification using IProgress<T>. Here is what I do:

  1. Create a Progress<T> and attach a handler to its ProgressChanged event.
  2. In the event handler, I update UI-specific property to show progress.
  3. Run a loop and create Task objects to do units of work. Each Task uses Progress object to report progress (which will in turn invoke ProgressChanged and update UI).
  4. Use Task.WaitAll() to block till all tasks have finished. Show a completion message at the end.

The code looks something like this:

IProgress<Tuple<DataRow, MyVM>> BakeProgress;
BakeProgress = new Progress<Tuple<DataRow, MyVM>>();

((Progress<Tuple<DataRow, MyVM>>)BakeProgress).ProgressChanged += (sender, args) =>
  _BakeProgress += 100.0 / AllDataRows.Length;

var BakeTasks = new List<Task>();
foreach (var dr in AllDataRows) {

  BakeTasks.Add(Task.Run(() =>
    var Baked = MyBakingFunc();

    BakeProgress.Report(new Tuple<DataRow, MyVM>(dr, Baked));

    return Baked;

Task.WaitAll(BakeTasks.ToArray());      //Shouldn't this wait

MessageBox.Show("Baking Completed");

To my surprise, this code hits MessageBox line before hitting ProgressChanged event handler even once. What am I doing wrong?

N.B. It DOES hit ProgressChanged event handler once for every Task instance, but only after hitting the Completed line.

N.B. Ah and I did read this, but since this is WPF, we do have a synchronization context in which the Progress object was created.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311362 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO