c# - Winform 没有完全处理

标签 c# .net

我有一个无法完全处理的表单菜单。下面是完整的表单代码。它是更大系统的一部分,因此在首次打开菜单之前打开和关闭其他窗体。

有一个表单计时器每秒触发一次并打印表单是否被处理。有一个按钮可以打开另一个表单、搜索并关闭菜单。搜索还有一个计时器,用于打印它是否已被处置。

当菜单打开时,调试输出符合预期

*********** (in main menu): Disposed False
*********** (in main menu): Disposed False

当我点击时,我会得到菜单和搜索的计时器滴答声

*********** (in main): Disposed True
*************** (in search) Disposed False

它表明 Menu 的第一个实例已被释放,但显然计时器仍在运行。当我退出 Search 并打开 Main 时,现在有两个 Main 计时器在运行

*********** (in main): Disposed True
*********** (in main): Disposed False

我可以继续这样做(单击以打开搜索并退出)并且运行的主要计时器的数量不断增加。我很困惑。这是 Main 的代码

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace Gui
{
public partial class Menu : Form
{
    private System.Windows.Forms.Timer timer1;
    private Button button1;
    private IContainer components;

    public Menu()
    {
        InitializeComponent();
    }

    private void Menu_Load(object sender, EventArgs e)
    {
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Debug.Print("*********** (in main): Disposed {0}", IsDisposed);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var search = new Search();
        search.Show();
        Close();
    }
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(11, 17);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(125, 32);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Menu
        // 
        this.ClientSize = new System.Drawing.Size(282, 253);
        this.Controls.Add(this.button1);
        this.Name = "Menu";
        this.Load += new System.EventHandler(this.Menu_Load);
        this.ResumeLayout(false);
    }
}
}

最佳答案

    this.timer1 = new System.Windows.Forms.Timer(this.components);

您似乎复制/粘贴了表单类的 Designer.cs 文件的内容。 InitializeComponent() 方法当然是样板文件。但是您没有做对,您忘记了实际使用 this.components 成员。它的存在只有一个原因,即处理表单类使用的任何组件。像定时器 1。对于您放在表单上的任何控件,它都是自动的,它们可以通过表单的控件成员找到回来,但组件需要额外的帮助。

所以不要只复制/粘贴 InitializeComponent(),您必须也复制/粘贴 Dispose() 方法:

    protected override void Dispose(bool disposing) {
        if (disposing && (components != null)) {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

现在,当您关闭表单时,计时器会停止计时。

关于c# - Winform 没有完全处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36845584/

相关文章:

c# - ASP.NET MVC 4 : Error received when trying to insert datetime field in Sql Server Database from input text box

c# - 如何在C#中获取对象的所有简单属性

c# - 如何在 sandcaSTLe xml 文档构建中排除不可浏览的成员

c# - 类似数组的结构

c# - 带有 DbContext 多级 Include() 的 AutoMapper ProjectTo

c# - 如何将 .NET 日期转换为 NSTimeInterval?

c# - 在单独的项目与单独的命名空间中组织代码

c# - 用于计数的信号量实现。当前正在运行的实例

c# - Html 到 pdf 缺少一些字符 (itextsharp)

c# - WinForms:进入主菜单后不会触发文本框离开事件