Collection was modified; enumeration operation may not execute

What’s likely happening is that SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading to that message. You can verify this by changing foreach(Subscriber s in subscribers.Values) To foreach(Subscriber s in subscribers.Values.ToList()) If I’m right, the problem will disappear. Calling subscribers.Values.ToList() copies the values of subscribers.Values to a separate … Read more

When to use volatile with multi threading?

Short & quick answer: volatile is (nearly) useless for platform-agnostic, multithreaded application programming. It does not provide any synchronization, it does not create memory fences, nor does it ensure the order of execution of operations. It does not make operations atomic. It does not make your code magically thread safe. volatile may be the single-most … Read more