BackgroundWorker.ReportProgress() not updating property and locking up the UI
- by Willem
i am using a backgroundWorker to do a long running operation:
BackgroundWorker backgroundWorker = new BackgroundWorker() { WorkerSupportsCancellation = true, WorkerReportsProgress = true };
backgroundWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
};
backgroundWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
someViewModel.SomeProperty.Add((SomeObject)args.UserState);
};
backgroundWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
someViewModel.SomeList.ForEach(x =>
{
someViewModel.SomeInterface.SomeMethod(backgroundWorker, someViewModel, someViewModel.SomeList, x);
});
};
backgroundWorker.RunWorkerAsync();
Then in SomeInterface.SomeMethod:
public void SomeMethod(BackgroundWorker backgroundWorker, SomeViewModel someViewModel//....)
{
//Filtering happens
backgroundWorker.ReportProgress(0, someObjectFoundWhileFiltering);
}
So, when it comes to:
backgroundWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
someViewModel.SomeProperty.Add((SomeObject)args.UserState);//Adding the found object to the Property in the VM
};
On the line someViewModel.SomeProperty.Add((SomeObject)args.UserState);, the set on the Property is not firering and the UI just locks up.
What am i doing wrong? Is this the correct way to update the UI thread?