How to remove all Items from ConcurrentBag?

前端 未结 6 1141
猫巷女王i
猫巷女王i 2021-01-31 13:30

How to clear the ConcurrentBag? it don\'t have any method like Clear or RemoveAll...

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 13:58

    Although it might not completely clear due to a potential race condition, this is sufficient:

    while (!myBag.IsEmpty) 
    {
       myBag.TryTake(out T _);
    }
    

提交回复
热议问题