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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:44:50+00:00 2026-05-24T16:44:50+00:00

I’ve got a good old InvalidOperationException being thrown with the standard message Collection was

  • 0

I’ve got a good old InvalidOperationException being thrown with the standard message

Collection was modified; enumeration operation may not execute.

The problem is, the enumerator isn’t modifying itself, for example:

private TRoute _copyRoute(TRoute route)
{
    TRoute tempRoute = new TRoute();
    tempRoute.Initialize(route.Resource);

    foreach (TVisit visit in route)
    {
       tempRoute.Add(visit);
    }
    tempRoute.EndLocation = route.EndLocation;
    return tempRoute;
}

My code is multi-threaded (circa 12-15 threads for this example) and each thread is supposed to be working on its own deep clone of a route. Obviously something is going wrong somewhere, but, my question is how do I track this down with so many threads? Reducing the number significantly stops the problem manifesting itself.

In this case my route instance is an IList so I can play around with adding things to the interface. Underneath it has it’s own List implementation.

EDIT

Just to add, I could ToArray() or ToList() this and maybe ignore the problem here but I don’t really want to do that, I want to locate the cause. For example:

If I change it to the following:

private TRoute _copyRoute(TRoute route)
{
    TRoute tempRoute = new TRoute();
    tempRoute.Initialize(route.Resource);

    foreach (TVisit visit in route.ToList())
    {
       tempRoute.Add(visit);
    }
    tempRoute.EndLocation = route.EndLocation;
    return tempRoute;
}

Then I fail on this Assert, because a chance has occurred just before ToList()… I need to try and find out where that change is occuring

TRoute tempRoute1 = CopyRoute(route1);
TRoute tempRoute2 = CopyRoute(route2);
Debug.Assert(tempRoute1.Count == route1.Count);
  • 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-24T16:44:51+00:00Added an answer on May 24, 2026 at 4:44 pm

    Here’s something you can use to wrap your IList<T> – it checks that it’s on the right thread on each write operation. Of course, it would still be unsafe to be iterating over this on one thread while writing on another, but I assume that’s not the problem. (You could always call CheckThread on all operations, not just the writing ones.)

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    
    class ThreadAffineList<T> : IList<T>
    {
        private readonly Thread expectedThread;
        private readonly IList<T> list;
    
        public ThreadAffineList(IList<T> list)
        {
            this.list = list;
            this.expectedThread = Thread.CurrentThread;
        }
    
        private void CheckThread()
        {
            if (Thread.CurrentThread != expectedThread)
            {
                throw new InvalidOperationException("Incorrect thread");
            }
        }
    
        // Modification methods: delegate after checking thread
        public T this[int index]
        {
            get { return list[index]; }
            set
            {
                CheckThread();
                list[index] = value;
            }
        }
    
        public void Add(T item)
        {
            CheckThread();
            list.Add(item);
        }
    
        public void Clear()
        {
            CheckThread();
            list.Clear();
        }
    
        public void Insert(int index, T item)
        {
            CheckThread();
            list.Insert(index, item);
        }
    
        public bool Remove(T item)
        {
            CheckThread();
            return list.Remove(item);
        }
    
        public void RemoveAt(int index)
        {
            CheckThread();
            list.RemoveAt(index);
        }
    
        // Read-only members
        public int Count { get { return list.Count; } }
        public bool IsReadOnly { get { return list.IsReadOnly; } }
    
        public IEnumerator<T> GetEnumerator()
        {
            return list.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        public bool Contains(T item)
        {
            return list.Contains(item);
        }
    
        public void CopyTo(T[] array, int arrayIndex)
        {
            list.CopyTo(array, arrayIndex);
        }
    
        public int IndexOf(T item)
        {
            return list.IndexOf(item);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.