Home Update ModalDialog progress bar from async await
Reply: 2

Update ModalDialog progress bar from async await

Vaibhav Lawand
Vaibhav Lawand Published in 2018-01-12 13:00:12Z

I am trying to understand better how to update ProgressBar Value using async and await.

I can update the progress bar value using async if progress bar is on UI main thread. But i am having modal dialog window which contains progress bar within it and this modal dialog pops up when I click on a button.

Now I want to update value of this progress bar from Async method.

How to update modal dialog progress bar from async method?

P.S - I don't want to use BackgroundWorker.

Lineker Reply to 2018-01-12 13:16:11Z

Assuming you are running a WPF app you can do that by calling the current dispatcher

private void PushOnUIThread(Action action)
            if (Application.Current.Dispatcher.CheckAccess())

Then you can call

PushOnUIThread(()=> progressBar.Value = 30);
JSteward Reply to 2018-01-12 14:04:11Z

I think what you're looking for can be accomplished with IProgress<T>. IProgress<T> and the default implementation Progress<T> is the way to report progress from another context.

Have a look at: Reporting Progress from Async Tasks

And a sample from the post by @StephenCleary

public async void StartProcessingButton_Click(object sender, EventArgs e)
  // The Progress<T> constructor captures our UI context,
  //  so the lambda will be run on the UI thread.
  var progress = new Progress<int>(percent =>
    textBox1.Text = percent + "%";

  // DoProcessing is run on the thread pool.
  await Task.Run(() => DoProcessing(progress));
  textBox1.Text = "Done!";

public void DoProcessing(IProgress<int> progress)
  for (int i = 0; i != 100; ++i)
    Thread.Sleep(100); // CPU-bound work
    if (progress != null)
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO