• Register
0 votes
29 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?

7.5k 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.

36.1k points

Related questions

0 votes
1 answer 22 views
22 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 42 views
42 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
0 votes
1 answer 69 views
69 views
Problem : I am trying to use the WCF4.0 template -REST. Also I am trying to make the method that uploads a file using the stream. The problem always occur at below line of code: Stream myserverStream = myrequest.GetRequestStream() ... connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
asked Dec 10, 2019 alecxe 7.5k points
1 vote
1 answer 33 views
33 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 33 views
33 views
Problem : I want to add the data as one by one row to my datagridview. But I am facing the following error: "Index was out of range. Must be non-negative and less than the size of the collection parameter name: index" Why am I facing this error ? ... ColumFive = Convert.ToString(sum); string[] row = new string[]{ ColumOne, ColumTwo, ColumThree, ColumFour, ColumFive }; myDataGridView.Rows.Add(row);
asked Dec 4, 2019 alecxe 7.5k points