的ObservableCollection和线程 [英] ObservableCollection and threading

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

问题描述

我在我的课有一个的ObservableCollection 。并进一步进入我的班级我有一个线程。从这个线索,我想添加到我的的ObservableCollection 。但我不能做到这一点:

I have an ObservableCollection in my class. And further into my class I have a thread. From this thread I would like to add to my ObservableCollection. But I can't do this:

这种类型的CollectionView不支持从一个线程从调度线程不同其SourceCollection变化。

请注意,这不是从UI线程发生,所以我不能够访问调度。

Note that this is not happening from the UI thread, so i don't have access to the dispatcher.

推荐答案

JaredPar的做法是有效的。这是值得考虑的另一种方法是使用一个线程安全的的ObservableCollection 而不是内置的的ObservableCollection 。那里有几个实现在那里,但萨沙理发的实现和的CLinq连续集合类有一些在我看来,更好的的。在内部,这些类基本上是使用由JaredPar概述的办法,但封装它集合类中。

JaredPar's approach is a valid one. Another approach which is worth considering is using a thread safe ObservableCollection instead of the built-in ObservableCollection. There's a few implementations out there, but Sasha Barber's implementation and the CLinq Continuous Collection class are some of the better ones in my opinion. Internally, these classes essentially use the approach outlined by JaredPar, but encapsulate it inside the collection class.

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

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