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

若如初见. 提交于 2019-12-29 07:35:15

问题


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


回答1:


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.




回答2:


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]");
            }


来源:https://stackoverflow.com/questions/7912052/system-invalidoperationexception-due-to-collection-modification-on-call-to-appli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!