应用程序退出集合已修改; 枚举操作可能不会执行

我得到这个错误集合被修改; 枚举操作可能不会执行。

我有3种形式。 这些是所有3的形式关闭事件,我做了一些研究,并了解到一些来自修改/显示的内容导致此错误

Form1中

private void btnExitl_Click(object sender, EventArgs e)
        {
            this.Close();   
        }

private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
    {

     if (DataDirty)
        {
            if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))

                 Application.Exit();
            else
                e.Cancel = true;
        }
        else

             Application.Exit();
    }

窗体2:

 private void btnCancel_Click(object sender, EventArgs e)
    {

        this.Close();
    }

private void frmInputFiles_FormClosing(Object sender,FormClosingEventArgs e){int plantid = StaticClass.GlobalValue; //Properties.Settings.Default.PlantId = plantid;

        Program.fPlant = new frmPlant(plantid);

        Program.fPlant.Show();
        e.Cancel = false;
        //this.Hide();
    }

Form3:

private void btnClose_Click(object sender,EventArgs e){this.Close(); }

private void frmVesselData_FormClosing(object sender,FormClosingEventArgs e){

        DialogResult result;
        int fileId = StaticClass.FileGlobal;
        if (DataDirty)
        {
            string messageBoxText = "You have unsaved data. Do you want to save the changes and exit the form?";
            MessageBoxButtons button = MessageBoxButtons.YesNo;
            string caption = "Data Changed";
            MessageBoxIcon icon = MessageBoxIcon.Question;
            result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == DialogResult.No)
            {
                Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);

                    Program.fInputFiles.Show();
                   //e.Cancel=true;

            }
            if (result == DialogResult.Yes)
            {
                e.Cancel = true;
                //return;

            }
        }
        else
        {
            Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);
                Program.fInputFiles.Show();
                //e.Cancel = false;

        }



    }

只有当我查看第三个表单(Form3)时才会发生。 Form1,Form2工作正常。 但是,如果我查看form3并尝试返回form1,则在form3的form1的结束事件中的某处

我的猜测是形式This.close()的btnExit_close事件

谢谢你


在关闭你的第一个表单的时候,在FormClosingEvent上试试这个

    private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
    {

    if (DataDirty)
    {
        if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        {
             this.Close();
             Application.Exit();
        }
        else
            e.Cancel = true;
    }
    else
         Application.Exit();
    }

先调用this.Close(),然后Application.Exit(),Application.Exit()终止所有进程并关闭()关闭主窗体


只需调用Environment.Exit(0);

链接地址: http://www.djcxy.com/p/35719.html

上一篇: Application Exit Collection was modified; enumeration operation may not execute

下一篇: How can I verify that Excel interop objects are cleaned up properly?