ObservableCollection 和线程 [英] ObservableCollection and threading

查看:30
本文介绍了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 不支持从不同于 Dispatcher 线程的线程更改其 SourceCollection.

请注意,这不会发生在 UI 线程中,因此我无法访问调度程序.

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

推荐答案

JaredPar 的方法是有效的.另一种值得考虑的方法是使用线程安全的 ObservableCollection 而不是内置的 ObservableCollection.有一些实现,但是 Sasha Barber 的实现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天全站免登陆