Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 771
Mahmoud Al-Qudsi Avatar asked Oct 27 '11 05:10

Mahmoud Al-Qudsi


2 Answers

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.

like image 159
Hans Passant Avatar answered Nov 11 '22 18:11

Hans Passant


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]");
            }
like image 43
john santora Avatar answered Nov 11 '22 18:11

john santora