• 大量界面刷新时手动Dispose也是有必要的


    在winform窗体上拖一个flowLayoutPane,一个Button,项目中再创建一个用户控件UcControl,用户控件上放几十个子控件

    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    DateTime startTime = DateTime.Now;
    
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < Controlcount; i++)
                    {
                        UcControl v = new UcControl();
                        v.Width = 275;
                        v.Height = 120;
                        flowLayoutPanel1.Controls.Add(v);
    
                    }
                    slExecutedTime.Text = (DateTime.Now - startTime).ToString();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }
            }

    使劲点按钮,过一会弹出创建句柄出错。解决办法,加上红色部分就好了,再使劲点也不会出问题。

    private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    DateTime startTime = DateTime.Now;
                    DeleteChilds(flowLayoutPanel1);
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < Controlcount; i++)
                    {
                        UcControl v = new UcControl();
                        v.Width = 275;
                        v.Height = 120;
                        flowLayoutPanel1.Controls.Add(v);
    
                    }
                    slExecutedTime.Text = (DateTime.Now - startTime).ToString();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }
            }
    
    
          private void DeleteChilds(Control control)
            {
                while (control.Controls.Count > 0)
                {
                    if (control.Controls[0] != null)
                    {
                        //DeleteChilds(control.Controls[0]);
                        control.Controls[0].Dispose();
                    }
                    
                }
            
            }
     
  • 相关阅读:
    linux(ubuntu) 系统修改/etc/fstab文件后无法进入系统的解决方法-摘录
    linux实现实时同步服务
    linux利用网易邮箱发送邮件
    企业数据库备份方案——mysqldump完全备份+binlog增量备份
    Nginx下隐藏index.php
    linux日志详解-摘录
    expect免交互用法
    删除超过多少天的日志文件或者备份文件
    Python_结合Re正则模块爬虫
    Jmeter性能分析
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/8494582.html
Copyright © 2020-2023  润新知