我正在使用 abackgroundWorker
进行长时间运行的操作:
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();
然后在SomeInterface.SomeMethod
:
public void SomeMethod(BackgroundWorker backgroundWorker, SomeViewModel someViewModel//....)
{
//Filtering happens
backgroundWorker.ReportProgress(0, someObjectFoundWhileFiltering);
}
因此,当涉及到:
backgroundWorker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
someViewModel.SomeProperty.Add((SomeObject)args.UserState);//Adding the found object to the Property in the VM
};
在线上someViewModel.SomeProperty.Add((SomeObject)args.UserState);
,设置SomeProperty
不开火,UI
只是锁定。
我究竟做错了什么?这是更新的正确方法UI thread
吗?