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 199671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:02:38+00:00 2026-05-11T17:02:38+00:00

Perhaps someone can point me in the correct direction, because I’m completely stumped on

  • 0

Perhaps someone can point me in the correct direction, because I’m completely stumped on this.

I have a function that simply prints out a LinkedList of classes:

    LinkedList<Component> components = new LinkedList<Component>();
    ...
    private void PrintComponentList()
    {
        Console.WriteLine("---Component List: " + components.Count + " entries---");
        foreach (Component c in components)
        {
            Console.WriteLine(c);
        }
        Console.WriteLine("------");
    }

The Component object actually has a custom ToString() call as such:

    int Id;
    ...
    public override String ToString()
    {
        return GetType() + ": " + Id;
    }

This function typically works fine – however I’ve run into the issue that when it builds to about 30 or so entries in the list, the PrintcomplentList foreach statement comes back with an InvalidOperationException: Collection was modified after the enumerator was instantiated.

Now as you can see I’m not modifying the code within the for loop, and I haven’t explicitly created any threads, although this is within an XNA environment (if it matters). It should be noted that the printout is frequent enough that the Console output is slowing down the program as a whole.

I’m completely stumped, has anyone else out there run into this?

  • 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-11T17:02:38+00:00Added an answer on May 11, 2026 at 5:02 pm

    I suspect the place to start looking will be at any places where you manipulate the list – i.e. insert/remove/re-assign items. My suspicion is that there will be a callback/even-handler somewhere that is getting fired asynchronously (perhaps as part of the XNA paint etc loops), and which is editing the list – essentially causing this problem as a race condition.

    To check if this is the case, put some debug/trace output around the places that manipulate the list, and see if it ever (and in particular, just before the exception) runs the manipulation code at the same time as your console output:

    private void SomeCallback()
    {
       Console.WriteLine("---Adding foo"); // temp investigation code; remove
       components.AddLast(foo);
       Console.WriteLine("---Added foo"); // temp investigation code; remove
    }
    

    Unfortunately, such things are often a pain to debug, as changing the code to investigate it often changes the problem (a Heisenbug).

    One answer would be to synchronize access; i.e. in all the places that edit the list, use a lock around the complete operation:

    LinkedList<Component> components = new LinkedList<Component>();
    readonly object syncLock = new object();
    ...
    private void PrintComponentList()
    {
        lock(syncLock)
        { // take lock before first use (.Count), covering the foreach
            Console.WriteLine("---Component List: " + components.Count
                  + " entries---");
            foreach (Component c in components)
            {
               Console.WriteLine(c);
            }
            Console.WriteLine("------");
        } // release lock
    }
    

    and in your callback (or whatever)

    private void SomeCallback()
    {
       lock(syncLock)
       {
           components.AddLast(foo);
       }
    }
    

    In particular, a “complete operation” might include:

    • check the count and foreach/for
    • check for existance and insert/remove
    • etc

    (i.e. not the individual/discrete operations – but units of work)

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

Sidebar

Related Questions

I'm not sure what this practice is actually called, so perhaps someone can edit
This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
Perhaps I am not asking or searching for this correctly: I want to have
I'm hoping someone can either tell me what I'm doing wrong correct my flawed
Perhaps not directly programming related, but definitely product / commercially related. And I can't
Perhaps this is a naive question. In my understanding, ASP.NET MVC cannot work with
Perhaps my question is similar in nature to this one: Do you use design
Or perhaps this is a manual install only deal.
Inspired by this topic , I decided to write a simple program that does
I'm apparently laboring under a poor understanding of Python scoping. Perhaps you can help.

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.