【发布时间】:2021-12-06 13:28:48
【问题描述】:
我有一个BlockingCollection:
private readonly BlockingCollection<ImageKeys> myCompletedImages;
我有一个向BlockingCollection 添加项目的方法:
public void addItem(ImageKey theImagekey)
{
if (this.myCompletedImages.Contains(theImagekey)) // Here it says
// I do not have "Contains"
{
return;
}
this.myCompletedImages.Add(theImagekey);
}
如何知道BlockingCollection 中是否存在特定元素?由于Contains 不存在.. 还有其他方法可以吗?
【问题讨论】:
-
即使你可以,这样的方法也没有用,因为它可以在你检查它是否存在并且在你添加项目本身之前添加。
-
System.Linq 中有一个扩展方法
Contains<>,你甚至可以提供一个IEqualityComparer<T> -
@Hazrelle 正如 Servy 已经解释的那样,这将是无用的,因为可能会在支票和
Add调用之间添加匹配项 -
@Mr.Curious 你想做什么?你为什么使用 BlockingCollection 而不是
ConcurrentDictionary?可能有更好的方法(和更好的集合)来做任何你想做的事情。您是否试图阻止向订阅者/工作人员发布重复消息? -
使用线程安全队列
标签: c# multithreading blockingcollection