Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/The complete guide to the invalidoperationexception: collection was modified; enumeration operation may not execute issue
Next
Answered
Karina Lam
  • 15
Karina Lam
Asked: May 18, 20222022-05-18T07:00:28+00:00 2022-05-18T07:00:28+00:00In: csharp

The complete guide to the invalidoperationexception: collection was modified; enumeration operation may not execute issue

  • 15

. Advertisement .

..3..

. Advertisement .

..4..

I get the error: invalidoperationexception: collection was modified; enumeration operation may not execute. when I try to run the program below:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
 public class SubscriptionServer : ISubscriptionServer
 {
  private static IDictionary<Guid, Subscriber> subscribers;
 
  public SubscriptionServer()
  { 
  subscribers = new Dictionary<Guid, Subscriber>();
  }
 
  public void NotifySubscribers(DataRecord sr)
  {
  foreach(Subscriber s in subscribers.Values)
  {
  try
  {
  s.Callback.SignalData(sr);
  }
  catch (Exception e)
  {
  DCS.WriteToApplicationLog(e.Message, 
  System.Diagnostics.EventLogEntryType.Error);
 
  UnsubscribeEvent(s.ClientId);
  }
  }
  }
  
  public Guid SubscribeEvent(string clientDescription)
  {
  Subscriber subscriber = new Subscriber();
  subscriber.Callback = OperationContext.Current.
  GetCallbackChannel<IDCSCallback>();
 
  subscribers.Add(subscriber.ClientId, subscriber);
  
  return subscriber.ClientId;
  }
 
  public void UnsubscribeEvent(Guid clientId)
  {
  try
  {
  subscribers.Remove(clientId);
  }
  catch(Exception e)
  {
  System.Diagnostics.Debug.WriteLine("Unsubscribe Error " + 
  e.Message);
  }
  }
 }

The error appears the system notifies as follows:

Collection was modified; enumeration operation may not execute

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

enumeration operation may not execute
  • 2 2 Answers
  • 32 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-05T04:07:40+00:00Added an answer on June 5, 2022 at 4:07 am

    The cause: SignalData is most likely modifying the subscribers dictionary invisibly under the hood during the loop, resulting in that message.

    The solution: You can solve this problem by changing:

    foreach(Subscriber s in subscribers.Values)

    To

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

     

    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Gabriel Giraud
    2022-05-25T20:10:53+00:00Added an answer on May 25, 2022 at 8:10 pm

    Unsubscribe a subscriber to change the contents of the Collection of Subscribers during Enumeration.

    This can be fixed in a number of ways, including changing the for loop to use an explicitly .ToList()

    public void NotifySubscribers(DataRecord sr) 
    {
     foreach(Subscriber s in subscribers.Values.ToList())
     {
     ^^^^^^^^^ 
     ...
    • 10
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.