BackgroundWorker任务状态源码实例

些代码的主要功能是 应用了BackgroundWorker 这个类
任务正在运行时柱塞客户操作,并且可以看到任务运行状态
任务完成窗口关闭

源码实例下载地址https://pan.baidu.com/s/1skGYzTF

以下是backgroundWorkerFrm窗体核心代码

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

namespace BackgroundWorker窗体状态
{
    public partial class backgroundWorkerFrm : Form
    {
        BackgroundWorker worker = new BackgroundWorker();
        public backgroundWorkerFrm()
        {
            InitializeComponent();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;

            worker.RunWorkerCompleted += CompleteWork;//操作完成就会执行CompleteWork事件
            worker.ProgressChanged += UpdateProgressBar;//进程发现变化时执行UpdateProgressBar事件
        }
        public DoWorkEventHandler DoWork { set { worker.DoWork += value; } }
        private void UpdateProgressBar(object sender, ProgressChangedEventArgs e)//进度条
        {
            this.progressBar1.Value = e.ProgressPercentage;
        }
        private void CompleteWork(object sender, RunWorkerCompletedEventArgs e)//关闭窗口
        {
            this.DialogResult = DialogResult.OK;
        }
        public void RunAndShow()
        {
            if (!worker.IsBusy)
            {
                worker.RunWorkerAsync();
                this.ShowDialog();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (worker != null && worker.IsBusy)
            {
                worker.CancelAsync();
            }

            //base.OnClosing(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (worker != null && worker.IsBusy)
            {
                worker.CancelAsync();
            }
        }
        public void SetLable(string msg)
        {
            this.BeginInvoke((EventHandler)(delegate { label1.Text = msg; }));
        }
    }
}
以下是Form1核心代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace BackgroundWorker窗体状态
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        backgroundWorkerFrm frm ;
        private void button1_Click(object sender, EventArgs e)
        {
            frm = new backgroundWorkerFrm();
            frm.DoWork = test;
            frm.RunAndShow();
        }

        private void test(object sendr, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sendr as BackgroundWorker;
            for (int i = 0; i <= 500; i++)
            {
                bw.ReportProgress(100 * i / 500);
                Thread.Sleep(50);//延时作用,如果不延时就会执行很快 进度条一下就刷到100%
                this.BeginInvoke((EventHandler)(delegate { label2.Text = i.ToString(); }));
                if (bw.CancellationPending)
                {
                    break;//取消时中断任务
                }
                if (i == 100)
                {
                    frm.SetLable("100");
                }
                if (i == 200)
                {
                    frm.SetLable("200");
                }
                if (i == 300)
                {
                    frm.SetLable("300");
                }
            }
        }
    }
}



评论