Periodically repeating a collection of ever-changing

I have a set of objects that are constantly changing, and I want to show some information about the contents so often (my application is multithreaded, and in different ways threads constantly send requests for changing the object in the collection, so this is unpredictable).

If I lock the collection, I can iterate through it and receive information without any problems, however, this causes problems with other threads, as they could submit several requests to change the collection at the same time and would be stalled. I have thought of several ways around this and I am looking for any advice.

  • Make a copy of the collection and repeat it so that the source text continues to be updated in the background. The collection can become large, so it is not perfect, but it is safe.
  • Iterate over it using a For ... Next loop and catch the IndexOutOfBounds exception if the item is removed from the collection during iteration. Sometimes this can lead to duplicates in my picture, so it’s also not perfect.

Any other ideas? I am concerned about the snapshot, so I don’t care about the reflection of changes in my application - my main problem is that the collection can be updated with minimal delay and that updates will never be lost.

+3
source share
3 answers

, System.Concurrent, .NET Framework 4. , , ConcurrentQueue<T>, . . , . , . . - .NET Framework 4.

http://msdn.microsoft.com/en-us/library/dd997305(v=VS.100).aspx

+2

, .ToArray() . , , ? , .

+1

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


All Articles