backgroundWorker使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace Aisino.Fwkp.Wbjk.Forms
{
    public partial class DGJForm : Form
    {
        public DGJForm ()
        {
            Initialize();

 

           //
            // backgroundWorker1
            //
            this.backgroundWorker1.WorkerReportsProgress = true;
            this.backgroundWorker1.WorkerSupportsCancellation = true;
            this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
            this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
            this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);

        }

        private void DGJForm _Load(object sender, EventArgs e)
        {
            this.label1.Text = "";
            this.backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                int CheckTotal = DanJus.Rows.Count;

             

              for (int i = 0; i < DanJus.Rows.Count; i++)
            {
                if (backgroundWorker.CancellationPending)   //查看用户是否取消该线程
                {
                    break;
                }
                System.Threading.Thread.Sleep(400);  //为了显示,减速   
                backgroundWorker.ReportProgress((100 * (i + 1)) / CheckTotal, DanJuBH);
            }

                e.Result = CheckTotal;
            }
            catch (Exception ex)
            {
                    //.......

            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.CancelAsync();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.label3.Visible = false;
            if (e.Cancelled)
            {
                MessageBox.Show("已取消");
            }
            else if (e.Error != null)
            {
                string msg = String.Format("发生错误: {0}", e.Error.Message);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                // 正常完成

            }
            this.Close();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
            this.label1.Text = e.UserState.ToString();
        }
    }
}

转载于:https://www.cnblogs.com/panjun/archive/2012/11/19/2777256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BackgroundWorker是一个多线程组件,可以在后台执行耗时的操作,而不会阻塞UI线程。以下是使用BackgroundWorker的步骤: 1. 在Visual Studio中拖动一个BackgroundWorker组件到窗体或控件中。 2. 设置BackgroundWorker的属性,包括是否支持取消操作、是否报告进度等。 3. 在代码中为DoWork事件编写处理程序,该事件是BackgroundWorker执行耗时操作的地方。在处理程序中执行需要在后台线程中进行的操作。 4. 如果需要报告进度,可以在处理程序中调用ReportProgress方法。在ProgressChanged事件中处理报告的进度。 5. 如果需要支持取消操作,可以在处理程序中检查CancellationPending属性,如果为true,则取消操作。 以下是一个示例,演示如何使用BackgroundWorker执行耗时操作并报告进度: ```csharp private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { int total = 100; for (int i = 0; i < total; i++) { if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } // 模拟耗时操作 Thread.Sleep(100); // 报告进度 int progress = (int)(((double)i / total) * 100); backgroundWorker1.ReportProgress(progress); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } private void button1_Click(object sender, EventArgs e) { // 启动后台操作 backgroundWorker1.RunWorkerAsync(); } private void button2_Click(object sender, EventArgs e) { // 取消后台操作 backgroundWorker1.CancelAsync(); } ``` 在上面的示例中,我们在DoWork事件处理程序中模拟了一个耗时操作,使用ReportProgress方法报告进度,并在ProgressChanged事件处理程序中更新进度条。在取消按钮的Click事件处理程序中,我们调用CancelAsync方法取消后台操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值