Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Crazy error

Crazy error

Scheduled Pinned Locked Moved C#
csharphelpquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jonathan Harker
    wrote on last edited by
    #1

    Hi all, I keep getting the following error message: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args) does anybody know what this means or where it could be originating from? BlueBroadcaster

    M D D 3 Replies Last reply
    0
    • J Jonathan Harker

      Hi all, I keep getting the following error message: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args) does anybody know what this means or where it could be originating from? BlueBroadcaster

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #2

      Actually, it's not crazy. It's probably something like :-

      foreach (Object obj in myCollection)
      {
      // some code

      myCollection.Add(anotherobj);
      
      // some more code
      

      }

      and it's telling you that you can't add / remove / clear or whatever in the collection you're iterating over. If you only ever want to add new items, you can use a "for (int loop=0; loop<myCollection.Count; ++loop)", although that will iterate over the new items as well. Otherwise, create "Add" and "Remove" lists which can be processed after the main loop to add and remove as needed.

      There are three kinds of people in the world - those who can count and those who can't...

      1 Reply Last reply
      0
      • J Jonathan Harker

        Hi all, I keep getting the following error message: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args) does anybody know what this means or where it could be originating from? BlueBroadcaster

        D Offline
        D Offline
        DavidRRR
        wrote on last edited by
        #3

        Sounds like Items was removed from your list before the line that causes error. hope usefull. sorry for bad english. :)

        1 Reply Last reply
        0
        • J Jonathan Harker

          Hi all, I keep getting the following error message: System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() at System.Windows.Forms.Application.ExitInternal() at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t) at System.Windows.Forms.Control.WndProcException(Exception e) at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at BlueBroadcaster_Ultimate.ProgramClass.Main(String[] args) does anybody know what this means or where it could be originating from? BlueBroadcaster

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Basically, you cannot alter a collection of items as you ForEach over it. You can make changes to the items inside the collection, but you cannot add or delete items to the collection itself.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          J 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Basically, you cannot alter a collection of items as you ForEach over it. You can make changes to the items inside the collection, but you cannot add or delete items to the collection itself.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            J Offline
            J Offline
            Jonathan Harker
            wrote on last edited by
            #5

            Dave Kreskowiak wrote:

            you cannot alter a collection of items as you ForEach over it

            why does this happen?

            C 1 Reply Last reply
            0
            • J Jonathan Harker

              Dave Kreskowiak wrote:

              you cannot alter a collection of items as you ForEach over it

              why does this happen?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I guess because the framework is keeping track of where you are in the collection, which assumes the collection will not change. If it does, what should foreach do ? you may have removed an item, how do you know if it has been iterated over ? Would you want it to be ? Easier just to say 'don't change this while I am working on it'. I suspect there's a more concrete reason than that, if you look into how it's implimented, but it seems obvious to me that it would create some situations where you could not predict the behaviour of foreach if you could alter the collection.

              Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups