在单独的线程中更新 ObservableCollection [英] Updating an ObservableCollection in a separate thread

查看:28
本文介绍了在单独的线程中更新 ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF 应用程序中,一个 ObservableCollection 由 LINQ to SQL 查询填充和更新.然后使用此 ObservableCollection 中的值更新 UI 对象.

In a WPF application an ObservableCollection is filled and updated by LINQ to SQL queries. Then UI objects are updated using values from this ObservableCollection.

通过 LINQ to SQL 查询更新此 ObservableCollection 的操作在单独的线程中执行是否可能且合理?

Is it possible and reasonable that operations of updating this ObservableCollection by LINQ to SQL queries were executed in a separate thread?

如果是,在这种情况下,它会是这个 ObservableCollection 的一个和同一个实例吗?(我的意思是,如果从 LINQ 数据上下文中获取值和提供值以更新 UI 的值不同,那么我将无法更新 UI)

If yes, will, in this case, it be one and the same instance of this ObservableCollection? (I mean, if it is not the same one for taking values from LINQ datacontext and the one for giving values to update UI, then I will not be able to update UI)

推荐答案

使用内置的 ObservableCollection 类,如果 UI 是绑定到集合,它会抛出一个 NotSupportedException(但集合项属性的更改通知工作正常).我写了一个 AsyncObservableCollection<T> class 来处理这种情况.它通过调用 UI 同步上下文上的事件处理程序来工作

With the built-in ObservableCollection<T> class, you can't change the content from a separate thread if the UI is bound to the collection, it throws a NotSupportedException (but change notification for properties of collection items works fine). I wrote an AsyncObservableCollection<T> class to handle this case. It works by invoking the event handlers on the UI synchronization context

这篇关于在单独的线程中更新 ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆