I'm trying to get at progress bar to update based on how many times a loop have been executed. The loop is running in it's own thread. I'm under the impression that Dispatcher.BeginInvoke
is a good choice and implemented it. I've also read that the dispatcher is able to update the GUI even though it's running its own thread. However, I'm not able to get this to work when trying to notify the binding about an update, implementing INotifyPropertyChanged
. I've seen some blogs, questions etc that suggest different magic methods to solve this, but since I've read this should be handled by the dispatcher I would like to avoid that.
NB! This is not an Observable collection, which I've read is not handled by the dispatcher.
Sample code from viewmodel
Application.Current.Dispatcher.BeginInvoke(() =>
{
this.Progress = 0;
for (int i = 0; i < this.nrOfLoops; i++)
{
// implementation goes here ...
if (i % 100 == 0) // Only update every 100 cycle
{
this.Progress = (double)i / nrOfLoops * 100;
this.NotifyPropertyChanged("Progress");
}
}
}, DispatcherPriority.Background);
XAML
<ProgressBar Value="{Binding Path=Progress}"/>
The GUI is not continuously updated, only filled to 100% when all is done. I've tried messing around with DispatcherPriority
just in case, but without results. I do know that the binding is ok from setting it to a default value, as well as the fact that the progress bar is filled when finished.
So... What am I doing wrong here? Or have I been misinformed about the dispatcher being able to handle this? It feels like this must be a very common scenario.