sura
sura

Reputation: 1511

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. I am not sure if trypeek would iterate through the collection. OTOH, copying the items to an array would not work because then the objects are read only. Please help

Upvotes: 0

Views: 3285

Answers (2)

Mario The Spoon
Mario The Spoon

Reputation: 4859

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.

Also there is no thread-safe iteration.

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

hth

Mario

Upvotes: 1

Roy Dictus
Roy Dictus

Reputation: 33139

You cannot modify items in-place, you have to take them out, modify them and put them back in.

Upvotes: 4

Related Questions