System.InvalidOperationException due to collection modification on call to Application.Exit()
Asked Answered
C

3

11

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..

Colourable answered 27/10, 2011 at 5:33 Comment(5)
It sounds like the Forms collection. IIRC I have seen this happen before, but cannot remember any details, sorry.Spoofery
What is your application doing at shutdown? Could you please provide the code.Southsouthwest
Nothing, really... At the time I pressed the exit button, the application was in an idle state, no background threads or activities running.Colourable
I have this problem because I have a form created on a thread other than the initial thread. Check each form's FormClosing and FormClosed event. Are any forms running on threads different to your main GUI?Penalty
Just noticed the "first chance exception" - are you sure you are looking at the right exception? (blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx)Illegalize
B
20

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.

Bloodhound answered 27/10, 2011 at 9:57 Comment(3)
I do have a rather convoluted FormClosing code that does sometimes open message boxes. At any rate, changing from Application.Exit() to this.Close() appears to have worked around the issues without requiring that I change the contents of FormClosing.Colourable
@Hans Passant, having the exact same issue; and sure enough, i displayed a message box in my Form_CLOSING method. And boom, same collection modification. How silly. Moved it to Form_CLOSED and good. Kinda odd, but hey, bravo on that result.Chorion
This was exactly my problem too, a MessageBox in OnClosing was invalidating the Application.Exit open forms iterator. Moving it to OnClosed fixed it!Gummous
T
1

We just spent days on this problem too... where we got the 'System.InvalidOperationException' exception, and the app (in this case using a twain library from DynamSoft). Apparently we should not have been calling the CLOSE() after calling application.exit. Commenting out the Close made the exception go away, and made the app end normally. Visually, the app would display a weird message box from Microsoft saying "would you like to submit more information about this problem" - WHAT PROBLEM? it didn't display anything before this, so off we went digging through stack traces.

                Utils.Logger.Info("", "AsystScanner/dynamicDotNetTwain2_OnPostAllTransfers");
                Utils.Logger.Info("Closing down application!", "AsystScanner/dynamicDotNetTwain2_OnPostAllTransfers");

                // caller should close down app, added 3/3/15
                dynamicDotNetTwain2.CloseSource();
                dynamicDotNetTwain2.CloseSourceManager();

                System.Windows.Forms.Application.Exit();
no no!  don't do a close here.
                //try
                //{
                //    Close();
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message + "  Routine=dynamicDotNetTwain2_OnPostAllTransfers/Close() statement failed. [EJS1503031630]");
                //}
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "  Routine=dynamicDotNetTwain2_OnPostAllTransfers [EJS1503031631]");
            }
Torsk answered 4/3, 2015 at 1:19 Comment(0)
S
0

This is fixed now. Will be available in Net9.

Seasoning answered 26/1 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.