List compilation has been modified to be robust

Is there a shell that completely removes this error? I have several loops for several threads, and I can’t predict whether the collection will change in the middle of foreach, I think this can be easily achieved by catching this specific error and getting a new handler for the collection and looping the remaining elements, however I’m not enough advanced to override the default behavior with such a function.

+4
source share
1 answer

Assuming you are using .NET 4, you can use one of the collections in System.Collections.Concurrent , such as ConcurrentBag . However, you did not specify any requirements, so it is difficult for you to find out if the ConcurrentBag itself will meet them.

If you read the thread safety documentation for most collections, you will find that they do not support secure modification from one thread while the collection is read (or modified) by another, without blocking. This is great to read from List<T> etc. Of several threads, if it is not modified by anything, but you should not have a single thread modifying it.

Ideally, try creating code to not require this unlimited write access for shared collections, but if you really need it, use the type that was designed for it.

+8
source

Source: https://habr.com/ru/post/1488963/


All Articles