Loading

winform BackgroundWorker组件操作

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading;
  9 using System.Windows.Forms;
 10 
 11 namespace BackgroundWorkTest
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         /// <summary>
 21         /// 开始运行按钮
 22         /// </summary>
 23         /// <param name="sender"></param>
 24         /// <param name="e"></param>
 25         private void btnStart_Click(object sender, EventArgs e)
 26         {
 27             if (backgroundWorker1.IsBusy)
 28             {
 29                 MessageBox.Show("正在运行线程");
 30                 return;
 31             }
 32 
 33             backgroundWorker1.RunWorkerAsync(1000);
 34         }
 35 
 36         /// <summary>
 37         /// 后台组件工作中运行的代码
 38         /// </summary>
 39         /// <param name="sender"></param>
 40         /// <param name="e">e.Argument传递的数值</param>
 41         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
 42         {
 43             int n = 0;
 44             int sum = 0;
 45             //获取异步操作的数值
 46             if (e.Argument != null)
 47             {
 48                 n = (int)e.Argument;
 49             }
 50 
 51             for (int i = 0; i < n; i++)
 52             {
 53                 //是否取消后台操作
 54                 if (backgroundWorker1.CancellationPending)
 55                 {
 56                     e.Cancel = true;
 57                     return;
 58                 }
 59                 sum += i;
 60                 Thread.Sleep(10);
 61                 //通过计算,传递值为100.比如i=1000,i / 10 = 100;
 62                 backgroundWorker1.ReportProgress(i / 10, "当前i的值为" + Convert.ToString(i));
 63             }
 64             //异步操作结果的值
 65             e.Result = sum;
 66         }
 67 
 68         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
 69         {
 70             //注意value数值不能大于100
 71             progressBar1.Value = e.ProgressPercentage;
 72             label1.Text = e.UserState.ToString();
 73         }
 74 
 75         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 76         {
 77             //异步操作期间是否出错
 78             if (!(e.Error == null))
 79             {
 80                 MessageBox.Show(e.Error.Message);
 81                 return;
 82             }
 83 
 84             //异步操作期间是否取消
 85             if (e.Cancelled)
 86             {
 87                 progressBar1.Value = 0;
 88                 label1.Text = "";
 89                 MessageBox.Show("取消线程的执行");
 90                 return;
 91             }
 92 
 93             label1.Text = "计算结果:" + e.Result.ToString();
 94          //   this.progressBar1.Value = 100;
 95         }
 96 
 97         /// <summary>
 98         /// 取消按钮
 99         /// </summary>
100         /// <param name="sender"></param>
101         /// <param name="e"></param>
102         private void btnCancel_Click(object sender, EventArgs e)
103         {
104             if (!backgroundWorker1.IsBusy) return;
105 
106             //取消后台操作
107             backgroundWorker1.CancelAsync();
108            
109         }
110     }
111 }

 

posted @ 2021-07-21 10:09  云辰  阅读(85)  评论(0编辑  收藏  举报