简体   繁体   English

修改并发包中的项目 + C#

[英]modifying items in a concurrentbag + C#

I am keeping a collection of objects in a concurrent bag.我将一组对象保存在并发包中。 depending on the received input, I want to modify these objects.根据收到的输入,我想修改这些对象。 How can this be done?如何才能做到这一点? I cannot use trytake because it is removing the item.我不能使用 trytake,因为它正在删除该项目。 I am not sure if trypeek would iterate through the collection.我不确定 trypeek 是否会遍历该集合。 OTOH, copying the items to an array would not work because then the objects are read only. OTOH,将项目复制到数组将不起作用,因为那时对象是只读的。 Please help请帮忙

You cannot modify items in-place, you have to take them out, modify them and put them back in.你不能就地修改项目,你必须把它们拿出来,修改它们,然后再放回去。

I think you have to roll your own thread-save colletction.我认为您必须推出自己的线程保存集合。 TryPeek returns an item, but in no way ensures that no other thread modifies it. TryPeek 返回一个项目,但绝不确保没有其他线程修改它。

Also there is no thread-safe iteration.也没有线程安全的迭代。

See here: http://www.codethinked.com/net-40-and-system_collections_concurrent_concurrentbag见这里: http://www.codethinked.com/net-40-and-system_collections_concurrent_concurrentbag

hth hth

Mario马里奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM