• Register
0 votes
43 views

Problem :

I am unable to understand the root cause of following error, because when I attach the debugger it will not occur.

Invalid operation exception: collection was modified; enumeration operation may not execute.                

This is the WCF server in a Window based service. The method NotifySubscribers() is always called by the service whenever there is a data event. When a Windows Forms client subscribes to it, then the subscriber ID is added to the subscribers dictionary, and when the client un subscribes, the ID is deleted from the dictionary. The error seems to happen when the client unsubscribes. When the next time the NotifySubscribers() method is called, the foreach() loop will fail with the error in the subject line. But when the debugger is attached and the client unsubscribes, the code executes fine.

Do you see a problem in my code? Do I need to make the dictionary the thread-safe?

6 5 3
7,540 points

1 Answer

0 votes

Solution :

I think that the SignalData is indirectly changing the subscribers dictionary under the hood during the loop and leading this is leading to that message. You can cross check it by changing

foreach(Subscriber s in subscribers.Values)

To

foreach(Subscriber s in subscribers.Values.ToList())

If I am not wrong after doing above changes your problem will disappear.

9 7 4
38,600 points

Related questions

0 votes
1 answer 23 views
23 views
Problem : I am new to C#. I want to know how can I get the value of a given key in a following exception which I am currently facing in C# in a way it affects all the generic classes? Below is the exception which I am currently facing. "The given key was not present in the dictionary." How to fix above exception.
asked Feb 24 mphil 2.3k points
0 votes
1 answer 48 views
48 views
Problem : I am having my radcombo machines on aspx page and whenever I try to select any option from them then it gives me following `Server Error in '/' Application. Exception Details: System.InvalidOperationException: Operation is not valid due to the current state of the object. My Version Information: Microsoft .NET Framework Version: 2.0.50727.3634 ASP.NET Version: 2.0.50727.3634
asked Dec 2, 2019 alecxe 7.5k points
1 vote
1 answer 39 views
39 views
Problem : I want to create the simple user control that should be the slider. When I try to add the AjaxToolkit SliderExtender to my user control I usually get below error: Server Error in '/' Application. The Controls collection cannot be modified because ... control and also tried adding the textbox and the slider extender to my placeholder programmatically but I am still facing the above error.
asked Dec 14, 2019 alecxe 7.5k points
0 votes
1 answer 9 views
9 views
Is there a standard algorithm or best practice on how to implement GetHashCode for my custom classes?
asked Aug 31 Sofi55 1.1k points
0 votes
1 answer 15 views