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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:08:16+00:00 2026-05-26T21:08:16+00:00

I am seeing a random exception Collection was modified; enumeration may not execute –

  • 0

I am seeing a random exception “Collection was modified; enumeration may not execute” – InvalidOperationException.

The exception points to foreach line in the code snippet below, I know this happens when a collection is modified while enumerating.

However in my scenario, I don’t see a real chance of it happening – UNLESS private member is not thread safe.. I may be wrong, but this where I need help to understand and figure out.

Here is how my code looks

I have code behind class which has a private collection like

private Dictionary<string, string> _someDictionary = SomeConstantClass.ConstantValue;

In the page prerender complete event, I am enumerating the dictionary

protected override void OnPagePreRenderComplete(object sender, EventArgs e){
     _someDictionary["AnotherKey"] = "Another value";

     foreach(var dataValuePair in _SomeDictionary){
         //Do some operation
     }
}

I also have a public property which can modify this collection, but it set in the ascx file like

<tc: UserControlA runat="server" id="abc" CustomProperty="true" />

and here is its implementation,

public bool CustomProperty{
    set{
         if (value)
            _someDictionary["CustomProperty"] = "Custom Value";
    }
}

It certainly modified my member variable collection – But as per my understanding this property should be fired and done in the Control Init itself.

So, I still dont see a scenario where the collection is modified during the pre render complete event.

Any idea what could cause the exception to occur??

other notes: the page certainly has many update panels though this specific usercontrol does not do anything fancy and does not even have postback scenario.
From the log I see that the issue is happening in HTTP GET request to the page.

Moreover: Suggest me a way (if any) to reproduce this.

For my friends who were interested to know the SomeConstantClass.ConstantValue, here it is

class SomeConstantClass{
  public static Dictionary<string, string> ConstantValue = new Dictionary<string, string> {
                      {"ABCD", "EFGH"},
                      {"HIJK", "LMNO"}
                   };
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:08:17+00:00Added an answer on May 26, 2026 at 9:08 pm

    If you are returning the same instance from SomeConstantClass.ConstantValue then multiple pages will have the private member variable pointing to the same object. This will lead to the object being changed on the init of one page while its being iterated on the OnPagePreRenderComplete of another page.

    Make sure that you return a new instance of the dictionary in each access to SomeConstantClass.ConstantValue. Example:

    public static Dictionary<string, string> ConstantValue
    {
        get
        {
            return new Dictionary<string, string>
            {
                {"ABCD", "EFGH"},
                {"HIJK", "LMNO"}
            };
        }
    }
    

    This way each page will have it own dictionary object to work with. This is the quick solution, you could refactor the logic so that you don’t need to have to create a new dictionary for each page.

    Basically private member variables are only thread safe if they reference an object that is private to that page and no one else knows about that object or the object itself is designed to be thread-safe. Encapsulating access to a non thread-safe static object through a private member will not make it thread safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seeing this: http://www.suckless.org/wiki.html . A wiki based on Mercurial. Are there any other non-code
I'm getting ( http://www.hdfgroup.org/projects/hdf.net/ ) The specified module could not be found. (Exception from
We are seeing random timeout issue when trying to invoke WCF Service through Classic
In trying to figure out whether or not my code's inner loop is hitting
I've seen quite a few recommendations for not seeding pseudo-random number generators more than
Sorry for this not being a real question, but Sometime back i remember seeing
Due to seeing random memory crashes in my iOS program, I decided to use
I have code that pics a random number from 0 to 1. I am
I'm running a Linux 2.6.36 kernel, and I'm seeing some random errors. Things like
I'm writting an ASP.NET MVC application and I'm seeing a seemly random performance from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.