(C#) BackgroundWorker updates UI controls

Time-consuming operations often block the UI process. Using BackgroundWorker to update UI controls can avoid the blocking of the main thread. The advantage is that the programming is simple and the logic is strong.

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. Initial conditions
            double Tao = (Vhot + JT + C6) / Rou;
            double H = KS4 / F;
            double G = (KS4 + KS5 + C6) / F;
            double PS = C6;
            double BeiT = 0;

            //2. Fill TK4, i=4
            i = 4;
            if (Tao + C4 / X + Ohm <= G)//Note the relationship between the two IFs
            {
                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))//Note the relationship between the two IFs
            {
                BeiT = C4 / (G - Ohm - Tao);
                Tao = Tao + C4 / BeiT;
                H = H + KS5 / F;
                G = G + C4 / F;
                PS=PS+C4;
                i++;
            }

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

                //4. Fill 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;//Process the result
        }

        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 = "";
        }

        //Calculation results
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            //create background worker process
            var bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;

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

            bw.RunWorkerAsync();
        }

        // clear input data
        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 = "";
        }

        //custom private function
        private int YBXN(int i)
        {
            int ret = 0;
            if (i % 3 != 0)
            {
                ret = i% 3 + 3;
            }
            else
            {
                ret = 6;
            }
            return ret;
        }
    }
}

Key code:

//create background worker process
 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() function: Time-consuming operation.

Worker_Completed() function: The operation after the time-consuming operation is completed, and the method for updating the UI control can be placed here.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326920247&siteId=291194637
Recommended