How to remove a single, specific object from a ConcurrentBag<>?

后端 未结 9 500
误落风尘
误落风尘 2021-02-02 04:34

With the new ConcurrentBag in .NET 4, how do you remove a certain, specific object from it when only TryTake() and TryPeek() are

9条回答
  •  迷失自我
    2021-02-02 05:20

    public static ConcurrentBag RemoveItemFromConcurrentBag(ConcurrentBag Array, String Item)
    {
        var Temp=new ConcurrentBag();
        Parallel.ForEach(Array, Line => 
        {
           if (Line != Item) Temp.Add(Line);
        });
        return Temp;
    }
    

提交回复
热议问题