Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've got this really, really weird error that I've never been able to pin down (it happens very rarely). Basically, I have a C# application that was randomly throwing an unknown exception on exit. I've managed to catch it in the debugger this time, and it turns out that calling Application.Exit() is throwing a System.InvalidOperationException with the following message:

A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll

Additional information: Collection was modified; enumeration operation may not execute.

I'm not sure what this collection that has been allegedly modified is, or who it was that modified it.

The stack trace isn't very helpful:

mscorlib.dll!System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() + 0x13f bytes System.Windows.Forms.dll!System.Windows.Forms.Application.ExitInternal() + 0x112 bytes System.Windows.Forms.dll!System.Windows.Forms.Application.Exit(System.ComponentModel.CancelEventArgs e) + 0x65 bytes

Any idea how I can find out which ArrayList it is that has been modified? I don't think it's anything I'm doing explicitly, more likely an action I'm doing that's modifying the underlying state of the .NET framework during the middle of an operation that MS wasn't expecting..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
792 views
Welcome To Ask or Share your Answers For Others

1 Answer

Unusual, never seen this before. The Application.ExitInternal() method iterates the Application.OpenForms collection. Clearly this collection is getting modified while it is doing so. There are few possible causes for this, I can only come up with one. One of your forms has overridden the OnFormClosing() method or subscribed the FormClosing event. And is doing something that modifies the OpenForms collection. Could be disposing the form object or creating a new form instance or modifying a form property that causes the window to be re-created, like ShowInTaskbar.

You won't find this code back in the call stack. Review your On/FormClosing code. Comment code out if you can't find it quickly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...