How to make BlockingCollection observable?
I've written a nice priority queue class,
class ConcurrentPriorityQueue<T>
: IProducerConsumerCollection<KeyValuePair<int,T>>, INotifyCollec开发者_StackOverflowtionChanged
where T : INotifyPropertyChanged
which I now want to wrap in a in a BlockingCollection
,
Queue = new ConcurrentPriorityQueue<DownloadItem>(10);
Buffer = new BlockingCollection<KeyValuePair<int, DownloadItem>>(Queue, 1000)
{
new KeyValuePair<int, DownloadItem>(0, new DownloadItem{Url = "stackoverflow.com"})
};
So that it can add a maximum capacity, and hopefully some thread-safety. Now, however, I seem to have lost the observable functionality!
How can I hook a DataGrid up to this collection so it still receive the collection changed notifications?
Binding to the underlying collection (the priority queue) seems to work. Then I just call Add
and Take
on the blocking collection instead. I guess that's why they decided to keep the objects separate.
BlockingCollection
does not implement INotifyCollectionChanged
interface that is essential for data binding (AFAIK). Looks like you have to roll up your own implementation (either inheriting from blocking collection or encapsulating it) that implements the said interface.
精彩评论