I'm having problems with the progress bar showing real-time updates.
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 appears blank when the function starts, and then nothing until the function finishes work. Can someone explain to me how this can be done? I am new to C # / WPF, so I am not 100% sure how to implement the dispatcher in another thread (as seen on some other posts) to fix this problem.
To clarify, my program has a button that, when clicked, captures a value from a text field and uses the API to extract information and creates labels on it. I want the progress bar to be updated after the completion of each row of data.
Here 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!
source
share