Wpf中ViewModel类里面经常会需要用到ObservableCollection来管理列表数据,在做异步通信的时候也会碰到“不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改”这样的异常,解决方法如下
ThreadPool.QueueUserWorkItem(delegate
{
System.Threading.SynchronizationContext.SetSynchronizationContext(new
System.Windows.Threading.DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
System.Threading.SynchronizationContext.Current.Post(pl =>
{
//里面写真正的业务内容
}, null);
});
本文介绍了解决WPF中使用ObservableCollection进行异步操作时遇到的线程更改异常的方法。通过设置DispatcherSynchronizationContext并使用Post方法,确保了UI线程与后台线程之间的正确同步。
2009

被折叠的 条评论
为什么被折叠?



