【问题标题】:Code executing after the form it is in has been closed and disposed在它所在的表单被关闭和处理之后执行的代码
【发布时间】:2019-10-27 01:12:17
【问题描述】:

我有一个 Windows 窗体应用程序以我不理解的方式运行。为了清晰起见,我创建了一个简单的应用程序来将功能重现到最小逻辑。它有两种形式。

表格1: 有一个按钮 1,单击该按钮会将行添加到数据表中。 有一个button2显示一个表单并将数据表发送到表单。

表格2: Form2 从数据表创建数据视图并将数据视图绑定到列表框。 Form2 还有一个 button1,它在表单上执行 this.Close()。我读到任何用 formName.Show() 打开的东西都会在 .Close 期间被处理掉。

这就是事情变得奇怪的地方。每次清除数据表并再次添加行时,都可以反复单击 Form1 button1。一旦单击 from1 button2 并显示 form2 然后关闭,返回到 form1 并单击 button1 会引发错误。错误来自 form2(已关闭)listBox1_SelectedIndexChanged 事件。

问题是,为什么在一个已消失的表单上为已消失的控件触发事件? 我找到了几种避免这种情况的方法,但我想了解为什么会发生这种情况,例如设置列表框 datasource = null 但想知道发生了什么......花了半天时间试图弄清楚这一点。 SO社区,请在这里教育我。

Form1 代码

public partial class Form1 : Form
{

    Boolean bInitialLoad = true;
    DataTable dtHardware = new DataTable("Hardware");
    Form2 multiServerView = new Form2();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dtHardware.Clear();

        if (bInitialLoad == true)
        {
            dtHardware.Columns.Add("ServerName", typeof(String));
            dtHardware.Columns.Add("Environment", typeof(String));
        }

        DataRow drNewRow = dtHardware.NewRow();
        drNewRow["ServerName"] = "SomeName";
        drNewRow["Environment"] = "SomeEnvironment";
        dtHardware.Rows.Add(drNewRow);

        drNewRow = dtHardware.NewRow();
        drNewRow["ServerName"] = "SomeName2";
        drNewRow["Environment"] = "SomeEnvironment2";
        dtHardware.Rows.Add(drNewRow);

        bInitialLoad = false;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenMultiServerView();
    }

    private void OpenMultiServerView()
    {
        multiServerView = new Form2(dtHardware);
        this.AddOwnedForm(multiServerView);
        multiServerView.Show();
    }
}

Form1 设计器

namespace WindowsFormsApp4
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(308, 390);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(107, 34);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(437, 296);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(102, 33);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(679, 482);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}

Form2 代码

public partial class Form2 : Form
{

    DataView dvServers = null;

    public Form2()
    {
        InitializeComponent();
    }

    public Form2(DataTable dt1)
    {
        InitializeComponent();

        dvServers = new DataView(dt1);
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        listBox1.DisplayMember = "ServerName";
        listBox1.DataSource = dvServers;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        System.Windows.Forms.FormCollection fc = Application.OpenForms;
        foreach (System.Windows.Forms.Form frm in fc)
        {
            if (frm.Name == "Form2")
            {
                return;
            }
        }
        MessageBox.Show("I am the listbox event from Form2");
    }

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

Form2 设计器

namespace WindowsFormsApp4
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(23, 113);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(749, 287);
            this.dataGridView1.TabIndex = 0;
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(291, 31);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(171, 69);
            this.listBox1.TabIndex = 1;
            this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(653, 415);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(118, 25);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.Load += new System.EventHandler(this.Form2_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button button1;
    }
}

【问题讨论】:

  • 为什么在 Form1 级别而不是在 OpenMultiServerView() 中声明 Form2 multiServerView?我认为这可能会使表格保持“活力”。
  • docsAddOwnedForm 状态 在调用 RemoveOwnedForm 方法之前,分配给所有者表单的表单保持拥有。也许可以试试。
  • @spodger 好问题。我需要出于其他原因,有时我需要从 form1 关闭和打开 form2。这就是它以这种方式声明的原因。
  • 表单可能已关闭,但我不认为它已被处理,它只是关闭了 ;)
  • 如果问题中的代码是最新版本Form1 仍然有两个对Form2 的引用。 Form2 无法收藏。

标签: c#


【解决方案1】:

这是因为Form2 的实例实际上并没有消失。假设它已被处理是正确的,因为您调用了Show,但垃圾收集器尚未收集它。事实上,它无法被收集,因为在提供的代码示例中,Form1 有两个对其创建的 Form2 实例的引用。

第一个在Form1.OwnedForms。第二个是字段Form1.multiServerView

您说您有几种方法可以修复它,例如在Form2 关闭时打破绑定,但我想我会放弃这个建议。如果您实际上不需要在每次显示时都创建一个 Form2 的新实例,您可以在 Form1 的构造函数中创建它的一个实例,处理 Form2.Closing 并隐藏 Form2 时用户关闭它。

所以,类似...

public partial class Form1 : Form
{
    //Removed bInitialLoad, we'll set up the data table in the constructor.
    DataTable dtHardware = new DataTable("Hardware");
    Form2 multiServerView; //No longer initalizing here, we'll do that in the constructor.

    public Form1()
    {
        InitializeComponent();

        dtHardware.Columns.Add("ServerName", typeof(String));
        dtHardware.Columns.Add("Environment", typeof(String));
        multiServerView = new Form2(dtHardware);
        AddOwnedForm(multiServerView);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dtHardware.Clear();

        //Removed the check of bInitialLoad.

        DataRow drNewRow = dtHardware.NewRow();
        drNewRow["ServerName"] = "SomeName";
        drNewRow["Environment"] = "SomeEnvironment";
        dtHardware.Rows.Add(drNewRow);

        drNewRow = dtHardware.NewRow();
        drNewRow["ServerName"] = "SomeName2";
        drNewRow["Environment"] = "SomeEnvironment2";
        dtHardware.Rows.Add(drNewRow);

        //Removed setting bInitialLoad.

    }

    private void button2_Click(object sender, EventArgs e)
    {
        OpenMultiServerView();
    }

    private void OpenMultiServerView()
    {
        multiServerView.Show(); //Just show it.
    }
}

public partial class Form2 : Form
{

    DataView dvServers = null;

    //Removed the empty constructor since Form1 no longer needs it.

    public Form2(DataTable dt1)
    {
        InitializeComponent();

        dvServers = new DataView(dt1);
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        listBox1.DisplayMember = "ServerName";
        listBox1.DataSource = dvServers;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("I am the listbox event from Form2");
    }

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

    private void Form2_Closing(object sender, FormClosingEventArgs e)
    {
        //Don't forget to wire it up in the designer!
        //If the user clicks "button1" or the "X" then just hide the form.
        if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true;
            Hide();
        }
    }
}

这样一来,Form2 就只有一个 Form2 的实例Form1。当Form1 上的某些内容更改数据源时,您不必担心已处置但尚未收集的Form2 实例会触发事件。

【讨论】:

  • 感谢您帮助我了解我错过了 Joshua 的垃圾收集点。顺便说一句,欢迎来到 SO。
  • 感谢您的欢迎,马修!很高兴我能提供帮助。
猜你喜欢
  • 1970-01-01
  • 2013-08-08
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 2013-09-11
  • 1970-01-01
相关资源
最近更新 更多