在BackgroundWorker类中完成长进程时,UI会冻结。 [英] UI freeze when long process done in BackgroundWorker class.

查看:79
本文介绍了在BackgroundWorker类中完成长进程时,UI会冻结。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,其中在flowLayoutPanel中创建超过700个标签在BackgroundWorker类的DoWork事件中完成的创建过程,但是当过程开始然后UI冻结。

我阅读并搜索关于BackgroundWorker类的所有教程,我遵循当Backgroundworker处理时,最终UI冻结。

我的代码是:



I am created an application where create more than 700 Labels in flowLayoutPanel. the creation process done in DoWork event of BackgroundWorker Class, but when the process started then the UI freeze.
I read and search all the tutorial about BackgroundWorker class and I follow the instruction, ultimately UI freeze when the Backgroundworker is in the processing.
My code is:

private void button1_Click(object sender, EventArgs e)
       {
           backgroundWorker1.RunWorkerAsync();
       }

       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
           List<Label> lblList = new List<Label>();
           for (int i = 1; i <= totaElement; i++)
           {
               Label lbl = new Label();
               lbl.Name = "lbl" + i;
               lbl.Text = i.ToString();
               lbl.BorderStyle = BorderStyle.FixedSingle;
               lbl.AutoSize = true;
               lblList.Add(lbl);
               backgroundWorker1.ReportProgress(i);
           }
           e.Result = lblList;
       }

       private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           int percentage = ((e.ProgressPercentage * 100) / 1000);
           progressBar1.Value = percentage;
       }

       private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
       {
           List<Label> CreatedLbl = (List<Label>)e.Result;
           flowLayoutPanel1.SuspendLayout();
           flowLayoutPanel1.Controls.AddRange(CreatedLbl.ToArray());
           flowLayoutPanel1.ResumeLayout();
       }





我下载 this [ ^ ]文章的演示并且它工作得很好,但我找不到我的错误..



如何解决这个问题?

如何释放用户界面?

感谢先进..



问候

Jayanta。



I download this[^] article's Demo and it's worked perfectly, but I couldn't find out my mistakes..

How to fix this problem?
How to free the UI ?
thanks in advanced..

Regards
Jayanta.

推荐答案

没有人说过在后台线程中创建UI控件是一个可怕的想法。不要养成它的习惯。它似乎可以在表面和测试中工作,但是你的控件可能无法正常工作。



应始终在UI线程上创建UI控件。



除此之外,创建1000个标签只会破坏表单重绘性能。
What nobody has said is that creating UI controls in a background thread is a horrible idea. Don't get into the habit of it. It may appear to work on the surface and in testing but your controls may not work properly.

UI controls should ALWAYS be created on the UI thread.

On top of that, creating 1000 labels is just going to kill your forms redraw performance.


这是我的最终代码:



Here is my final code:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {            
            for (int i = 1; i <= 1000; i++)
            {
                Label lbl = new Label();
                lbl.Name = "lbl" + i;
                lbl.Text = i.ToString();
                lbl.BorderStyle = BorderStyle.FixedSingle;
                lbl.AutoSize = true;
                lbl.BackColor = Color.LightSalmon;
                lbl.ForeColor = Color.DarkViolet;            
                Thread.Sleep(50);
                backgroundWorker1.ReportProgress(i,lbl);
            }            
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            int percentage = ((e.ProgressPercentage * 100) / 1000);
            progressBar1.Value = percentage;
            Label lbl =(Label) e.UserState;
            flowLayoutPanel1.SuspendLayout();
            flowLayoutPanel1.Controls.Add(lbl);
            flowLayoutPanel1.ResumeLayout();
        }





我更改了flowLayoutPanel1更新方法,它更新了工作线程上的每个Label创建,并且还发送了工作线程睡眠50毫秒(同时UI可以正确更新)..

并感谢@ F-ES Sitecore的理念......



I changed the flowLayoutPanel1 update method, it updated on every Label creation on worker thread and also sending worker thread to sleep for 50 milliseconds(in the meantime the UI can update properly)..
and thanks to @F-ES Sitecore for the Idea...


这篇关于在BackgroundWorker类中完成长进程时,UI会冻结。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆