Collection was modified; enumeration operation may not execute #10897

I see there are already a lot of questions about this error. I've read through all of them and pretty much every single one is because they are modifying a collection while enumerating over it, which is pretty obvious. However, my case is different.

So here is the exception in question. Note the last line I've modified.

Source    mscorlib
Method    MoveNext
Message   Collection was modified; enumeration operation may not execute.
Exception System.InvalidOperationException
Stack
   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(IntPtr 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.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at RandomEventWithinMyApp()

I personally have never seen the error or been able to reproduce it. I've only ever seen this error report come in from time to time from users and it has always eluded me.

As you can see, the error is happening within the System.Windows.Forms.Form.ShowDialog, so something is going wrong when trying to open a form. Obviously I don't have a function called RandomEventWithinMyApp, instead the error literally occurs at random in a control event, usually a button click for example as that normally triggers the opening of some form by the user.

Now if the error consistently happened when the user clicks the same button to open the same particular form, then it would be a lot easier to work out. The problem is that the event that triggers the error is random, it can be any event (such as a button click) that opens a form anywhere in the app. The form that is being opened when the error occurs is also random. So I have nothing to go by.

It almost feels like there is something else going wrong within the .Net form loading code that I can't see. Perhaps something that manages the collection of open forms or something... no idea, just wild guesses at this point. Does anyone have any ideas of where I should start investigating?

1 answer

  • answered 2017-11-15 00:18 Ceres

    I would check any form closing events or even form closed events. You can browse the .Net source on MSDN and see that the in Application.ExitIntenal it is iterating over an arraylist of forms to raise FormClosing events. Something may be going on in your form closing events that is modifying that arraylist that is being iterated.

                    // Raise the FormClosing and FormClosed events for each open form
                    if (forms != null) {
                        foreach (Form f in OpenFormsInternal) {
                            if (f.RaiseFormClosingOnAppExit()) {
                                cancelExit = true;
                                break; // quit the loop as soon as one form refuses to close
                            }
                        }
                    }