码迷,mamicode.com
首页 > 编程语言 > 详细

搞定多线程自动代码生成控件的问题,解决了参数不匹配异常

时间:2016-12-20 07:24:08      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:array   设置   ima   out   tab   oid   void   ges   代码   

技术分享

直接帖核心代码

 private void button1_Click(object sender, EventArgs e)
        {
            this.tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.RowCount = 10;
            this.tableLayoutPanel1.ColumnCount = 10;
            Button btn;
            List<Button> lst = new List<Button>();
            Thread objThread3 = new Thread(() =>
            {
                for (int i = 0; i < 50; i++)
                {
                    btn = new Button()
                    {
                        Name = i.ToString(),
                        Text = i.ToString(),
                        Size = new Size(50, 50),
                    };
                    lst.Add(btn);
                }

                if (this.tableLayoutPanel1.InvokeRequired)
                {
                    Thread.Sleep(500);
                    this.tableLayoutPanel1.Invoke(new Action<List<Button>>(setControls),lst);
                }
            });
            objThread3.IsBackground = true;//设置为后台线程(通常都要这样设置)
            objThread3.Start();
        }

        private void setControls(List<Button> lst )
        {
            tableLayoutPanel1.Controls.AddRange(lst.ToArray());
        }

 

搞定多线程自动代码生成控件的问题,解决了参数不匹配异常

标签:array   设置   ima   out   tab   oid   void   ges   代码   

原文地址:http://www.cnblogs.com/rxhunter/p/6201179.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!