Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a progress bar update in real time in wpf

I'm having some trouble making the progress bar show the updates in real time.

This is my code right now

for (int i = 0; i < 100; i++)
{
     progressbar1.Value = i;
     Thread.Sleep(100);
}

But for some reason the progress bar shows empty when the function runs, and then nothing until the function finishes running. Can someone explain to me how this can be done? I'm new to C#/WPF so I'm not 100% sure on how I would implement a Dispatcher on a different thread (as seen on some other posts) to fix this problem.

To clarify, my program has a button which when press, grabs the value from a textbox, and uses an API to retrieve info, and create labels based on it. I want the progress bar to update after every row of data is finished processing.

This is what I have right now:

private async void search(object sender, RoutedEventArgs e)
{
    var progress = new Progress<int>(value => progressbar1.Value = value);
    await Task.Run(() =>
    {
        this.Dispatcher.Invoke((Action)(() =>
        {
             some pre-processing before the actual for loop occur
             for (int i = 0; i < numberofRows; i++)
             {
                  label creation + adding
                  ((IProgress<int>)progress).Report(i);
             }
        }));
    });
}

Thank you!

like image 420
nonion Avatar asked Apr 26 '15 01:04

nonion


People also ask

How to implement ProgressBar in WPF?

The ProgressBar tag in XAML represents a WPF ProgressBar control. The Width and Height properties represent the width and the height of a ProgressBar. The Name property represents the name of the control, which is a unique identifier of a control.

How use ProgressBar in WPF for long running tasks?

Long running tasks in any application make the application or software nonresponsive. So to keep the user updated about the running task and also keep the application responsive during long running tasks we can use different kinds of loading bar options like. Ring Bar etc.

What is ProgressBar in C #?

A ProgressBar control is used to represent the progress of a lengthy operation that takes time where a user must wait for the operation to be finished.


2 Answers

If you are using .NET 4.5 or later, you can use async/await:

var progress = new Progress<int>(value => progressBar.Value = value);
await Task.Run(() =>
{
    for (int i = 0; i < 100; i++)
    {
        ((IProgress<int>)progress).Report(i);
        Thread.Sleep(100);
    }
});

You need to mark your method with async keyword to be able to use await, for example:

private async void Button_Click(object sender, RoutedEventArgs e)
like image 187
Aleksey Shubin Avatar answered Sep 19 '22 12:09

Aleksey Shubin


Managed to make it work. All I needed to do is instead of making it just

progressBar1.value = i;

I just had to do

progressbar1.Dispatcher.Invoke(() => progressbar1.Value = i, DispatcherPriority.Background);
like image 29
nonion Avatar answered Sep 20 '22 12:09

nonion