Collection was modified; enumeration operation may not execute

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Collection was modified; enumeration operation may not execute

Message par ForumBot »

Collection was modified; enumeration operation may not execute
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Collection was modified; enumeration operation may not execute

Message par ForumBot »

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 list at the start of the `foreach`. Nothing else has access to this list (it doesn't even have a variable name!), so nothing can modify it inside the loop.
Répondre

Revenir à « .NET & C# »