(C#)BackgroundWorker更新UI控件

标签: C#
16人阅读 评论(0) 收藏 举报

耗时运算时常会阻塞UI进程,利用BackgroundWorker更新UI控件,可以避免主线程的阻塞。优点是编程简单,逻辑性强。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShortTermScheduling
{
    public partial class Form1 : Form
    {
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            int i = 0;
            int j = 0;
            int k = 0;
            double CJ = 0;
            double CK = 0;
            double Vhot = Double.Parse(this.txtVhot.Text.Trim());
            double JT = Double.Parse(this.txtJT.Text.Trim());
            double C4 = Double.Parse(this.txtC4.Text.Trim());
            double C5 = Double.Parse(this.txtC5.Text.Trim());
            double C6 = Double.Parse(this.txtC6.Text.Trim());
            double X = Double.Parse(this.txtX.Text.Trim());
            double Rou = Double.Parse(this.txtRou.Text.Trim());
            double Ohm = Double.Parse(this.txtOhm.Text.Trim());
            double F = Double.Parse(this.txtF.Text.Trim());
            double KS4 = Double.Parse(this.txtG4.Text.Trim());
            double KS5 = Double.Parse(this.txtG5.Text.Trim());

            //1. 初始条件
            double Tao = (Vhot + JT + C6) / Rou;
            double H = KS4 / F;
            double G = (KS4 + KS5 + C6) / F;
            double PS = C6;
            double BeiT = 0;

            //2. 填充TK4, i=4
            i = 4;
            if (Tao + C4 / X + Ohm <= G)//注意两个IF的关系
            {
                Tao = Tao + C4 / X;
                H = H + KS5 / F;
                G = G + C4 / F;
                PS = PS + C4;
                i++;
            }
            else if ((Tao + C4 / Rou + Ohm <= G) && (G < Tao + C4 / X + Ohm))//注意两个IF的关系
            {
                BeiT = C4 / (G - Ohm - Tao);
                Tao = Tao + C4 / BeiT;
                H = H + KS5 / F;
                G = G + C4 / F;
                PS = PS + C4;
                i++;
            }

            do
            {
                //3. 赋值
                j = YBXN(i);
                k = YBXN(i + 1);

                //4. 填充TKj
                switch (j)
                {
                    case 4:
                        CJ = C4;
                        break;
                    case 5:
                        CJ = C5;
                        break;
                    case 6:
                        CJ = C6;
                        break;
                }
                switch (k)
                {
                    case 4:
                        CK = C4;
                        break;
                    case 5:
                        CK = C5;
                        break;
                    case 6:
                        CK = C6;
                        break;
                }
                if ((H <= Tao) && (Tao + CJ / X + Ohm <= G))
                {
                    Tao = Tao + CJ / X;
                    H = H + CK / F;
                    G = G + CJ / F;
                    PS = PS + CJ;
                    i++;
                }
                else if ((H <= Tao) && ((Tao + CJ / Rou + Ohm) <= G) && (G < Tao + CJ / X + Ohm))
                {
                    BeiT = CJ / (G - Ohm - Tao);
                    Tao = Tao + CJ / BeiT;
                    H = H + CK / F;
                    G = G + CJ / F;
                    PS = PS + CJ;
                    i++;
                }
            } while (H <= Tao);

            e.Result = PS;//处理结果
        }

        private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void Worker_Completed(object sender, RunWorkerCompletedEventArgs e)
        {
            this.txtPuSai.Text = e.Result.ToString();
        }

        public Form1()
        {
            InitializeComponent();

            this.txtVhot.Text = "25000";
            this.txtOhm.Text = "4";
            this.txtJT.Text = "18000";
            this.txtC4.Text = "34000";
            this.txtC5.Text = "34000";
            this.txtC6.Text = "20000";
            this.txtRou.Text = "625";
            this.txtF.Text = "334";
            this.txtX.Text = "420";
            this.txtG4.Text = "16000";
            this.txtG5.Text = "26000";
            this.txtPuSai.Text = "";
        }

        //计算结果
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            //创建后台工作进程
            var bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;

            bw.DoWork += Worker_DoWork;
            bw.ProgressChanged += Worker_ProgressChanged;
            bw.RunWorkerCompleted += Worker_Completed;

            bw.RunWorkerAsync();
        }

        //清除输入数据
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtC4.Text = "";
            this.txtC5.Text = "";
            this.txtC6.Text = "";
            this.txtG4.Text = "";
            this.txtG5.Text = "";
            this.txtF.Text = "";
            this.txtJT.Text = "";
            this.txtOhm.Text = "";
            this.txtVhot.Text = "";
            this.txtX.Text = "";
            this.txtRou.Text = "";
            this.txtPuSai.Text = "";
        }

        //自定义的私有函数
        private int YBXN(int i)
        {
            int ret = 0;
            if (i % 3 != 0)
            {
                ret = i % 3 + 3;
            }
            else
            {
                ret = 6;
            }
            return ret;
        }
    }
}

关键代码:

 //创建后台工作进程
 var bw = new BackgroundWorker();
 bw.WorkerReportsProgress = true;
 bw.WorkerSupportsCancellation = true;

 bw.DoWork += Worker_DoWork;
 bw.ProgressChanged += Worker_ProgressChanged;
 bw.RunWorkerCompleted += Worker_Completed;

 bw.RunWorkerAsync();

Worker_DoWork()函数:耗时的操作。

Worker_Completed()函数:耗时操作完成之后的操作,这里可以放置更新UI控件的方法。

查看评论

【吴刚】UI天气图标及控件设计标准视频教程

本套教程在学员有基本的PS和AI软件基础的基础上,循序渐进,深入浅出,全篇干货,系统化的讲解UI天气图标及控件设计的特点、技术实现方法及应用场景,以及通过逐个icon案例绘制方法实操,结合移动端的用户交互使用规范,以及相关的技术实现方法手把手教授学生UI天气图标及控件设计和表现技巧,帮助学者学以致用,举一反三。
  • 2018年03月27日 19:12

使用BackgroundWorker在UI上操作主线程控件

 凡是WinForm的应用程序,如果他执行了一个的非常冗长的处理操作(比如文件查询),它在执行时会锁定用户界面,虽然主活动窗口 一直在运行,但用户无法与程序交互,无法移动窗体或改变窗体大小,所以...
  • babauyang
  • babauyang
  • 2014-05-14 14:44:20
  • 3806

BackgroundWorker异步更新界面

BackgroundWorker类似于线程,但又不太一样,因为线程不可以直接更新界面,而BackgroundWorker可以。代码如下 private void buttonTest_Click(...
  • xxdddail
  • xxdddail
  • 2017-07-21 09:26:21
  • 411

通过C# BackgroundWorker创建工作线程并更新界面

http://msdn.microsoft.com/zh-cn/library/vstudio/system.componentmodel.backgroundworker(v=vs.110).asp...
  • cocoasprite
  • cocoasprite
  • 2013-11-23 16:38:34
  • 1408

C#中 使用 Backgroundworker控制UI线程 防止界面假死情况

在C#.net窗体编程中,非主线程调用该UI界面的时候往往会出现一些界面无响应或者假死的情况,我们可以用Backgroundworker很好的解决这一个问题。第一步,我们先建立一个Windows应用程...
  • myxx520
  • myxx520
  • 2010-02-05 08:42:00
  • 7412

WPF 利用BackgroundWorker 动态加载UI

BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += (ee, se) => {...
  • haifengzhilian
  • haifengzhilian
  • 2014-09-12 11:40:32
  • 3389

用Timer还是用BackgroundWorker实现定时更新画面的功能

用Timer还是用BackgroundWorker实现定时更新画面的功能 标签: timerobjectrandomstring工作class 2010-02-10 17:47 974人阅...
  • kasama1953
  • kasama1953
  • 2016-09-23 02:30:39
  • 1516

C# BackgroundWorker分析以及使用

namespace System.ComponentModel {     //     // 摘要:     //     在单独的线程上执行操作。     [DefaultEvent("...
  • u012343179
  • u012343179
  • 2016-07-16 11:38:34
  • 963

非常实用的C# backgroundworker使用方法

# BackgroundWorker 控件的几个实例(C# backgroundworker使用方法):在 WinForms 中,有时要执行耗时的操作,在该操作未完成之前操作用户界面,会导致用户界面停...
  • songkexin
  • songkexin
  • 2011-02-11 11:35:00
  • 39640

BackgroundWorker组件和Thread更新UI界面对比

  • 2011年10月10日 23:45
  • 46KB
  • 下载
    个人资料
    持之以恒
    等级:
    访问量: 1447
    积分: 140
    排名: 114万+
    文章存档