I have a concurrent BlockingCollection
with repeated elements. How can modify it to add or get distinct elements?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
The default backing store for
BlockingCollection
is aConcurrentQueue
. As somebody else pointed out, it's rather difficult to add distinct items using that.However, you could create your own collection type that implements
IProducerConsumerCollection
, and pass that to theBlockingCollection
constructor.Imagine a
ConcurrentDictionary
that contains the keys of the items that are currently in the queue. To add an item, you callTryAdd
on the dictionary first, and if the item isn't in the dictionary you add it, and also add it to the queue.Take
(andTryTake
) get the next item from the queue, remove it from the dictionary, and return.I'd prefer if there was a concurrent
HashTable
, but since there isn't one, you'll have to do withConcurrentDictionary
.