XYZ

任重而道远

(C#)BackgroundWorker更新UI控件

耗时运算时常会阻塞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控件的方法。

阅读更多

扫码向博主提问

去开通我的Chat快问

zyxhangiian123456789

技术无价
  • 擅长领域:
  • C#
  • STM32
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyxhangiian123456789/article/details/79972684
上一篇基于STM32的OV7725摄像头拍照实验
下一篇(C#)数字图像识别
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭