"Collection was modified; enumeration operation may not execute." on form disposal.

Posted by cyclotis04 on Stack Overflow See other posts from Stack Overflow or by cyclotis04
Published on 2010-05-18T21:30:35Z Indexed on 2010/05/18 22:10 UTC
Read the original article Hit count: 296

"Collection was modified; enumeration operation may not execute." appears to be a common error with foreach loops, but I can't figure mine out. I have two classes of forms. One is begun on startup, and a button creates new instances of the second form, and displays them. When I close the secondary forms, I get an InvalidOperationException.

FirstForm.cs

public partial class FirstForm : Form
{
    SecondForm frmSecond;
    ...
    private void button1_Click(object sender, EventArgs e)
    {
        frmSecond= new SecondForm ();
        frmSecond.Show();
    }
}

SecondForm.designer.cs

partial class SecondForm
{
    ...
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing); // InvalidOperationException thrown here.
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms