批量处理任务进度条控制—基于BackgroundWorker

      今天要做一个批量处理图层数据的功能,希望在处理任务过程中,各个任务都能在进度条中显示自己的当前进度,决定继续使用强大易用的BackgroundWorker组件。通过在RunWorkerCompleted事件中进行控制来达到各任务独立显示完成进度的目标。测试在之前的介绍BackgroundWork功能的测试代码基础上展开,测试例子截图如下:

                                                   批量处理任务进度条控制—基于BackgroundWorker_第1张图片

分别向每个ListBox中写入1-100000的数字,向每个Listbox中写入过程中,进度条都自左向右走一遍,直到3个ListBox写入数字任务全部完成。代码如下:

 

 1  public partial class Form1 : Form
 2     {
 3         List m_NumberBoxList = new List();//存放ListBox
 4         ListBox m_CurrentListBox = null;//当前写入Items的ListBox
 5         int m_CurrentListBoxIndex = 0;//当前写入Items的ListBox的索引
 6         public Form1()
 7         {
 8             InitializeComponent();
 9 
10             backgroundWorker1.WorkerReportsProgress = true;//报告完成进度 
11             backgroundWorker1.WorkerSupportsCancellation = true;//允许用户终止后台线程 
12             backgroundWorker1.DoWork += new DoWorkEventHandler(ListNumber);
13 
14             m_NumberBoxList.Add(listBox1);
15             m_NumberBoxList.Add(listBox2);
16             m_NumberBoxList.Add(listBox3);
17         }
18 
19         private void btnOk_Click(object sender, EventArgs e)
20         {
21             //向第一个ListBox写入数值
22             m_CurrentListBox = m_NumberBoxList[0];
23             if (!backgroundWorker1.IsBusy)//判断backgroundWorker1是否正在运行异步操作 
24             {
25                 backgroundWorker1.RunWorkerAsync(100000);//开始执行后台操作 
26             } 
27         }
28 
29         void ListNumber(object sender, DoWorkEventArgs e)
30         {
31             int num = (int)e.Argument;//接收传入的参数 
32             for (int i = 1; i <= num; i++)
33             {
34                 if (backgroundWorker1.CancellationPending)//判断是否请求了取消后台操作 
35                 {
36                     e.Cancel = true;
37                     return ;
38                 }
39                 backgroundWorker1.ReportProgress(i * 100 / num, i);//报告完成进度 
40                 System.Threading.Thread.Sleep(0);//后台线程交出时间片 
41             }
42         }
43 
44         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
45         {
46             progressBar1.Value = e.ProgressPercentage;//将完成进度数据传给进度条 
47             m_CurrentListBox.Items.Add(e.UserState);//将中间计算结果在ListBox控件中显示出来 
48             m_CurrentListBox.Update();//刷新ListBox
49         }
50 
51         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
52         {
53             progressBar1.Value = 0;
54             m_CurrentListBoxIndex++;
55 
56             if(m_CurrentListBoxIndex!=m_NumberBoxList.Count)
57             {
58                 m_CurrentListBox = m_NumberBoxList[m_CurrentListBoxIndex];
59                 if (!backgroundWorker1.IsBusy)//判断backgroundWorker1是否正在运行异步操作 
60                 { 
61                     backgroundWorker1.RunWorkerAsync(100000);//开始执行后台操作 
62                 } 
63             }
64             else 
65             {
66                 m_CurrentListBoxIndex = 0;
67                 MessageBox.Show("处理完成!");
68             }
69         }
70 
71         private void btnStop_Click(object sender, EventArgs e)
72         {
73             if (backgroundWorker1.WorkerSupportsCancellation == true)
74             {
75                 backgroundWorker1.CancelAsync();//取消后台操作 
76                 backgroundWorker1.Dispose();//释放资源 
77             }
78         }
79     }
View Code

运行过程截图如下:

批量处理任务进度条控制—基于BackgroundWorker_第2张图片 

转载于:https://www.cnblogs.com/RoyYu/p/3382263.html

你可能感兴趣的:(批量处理任务进度条控制—基于BackgroundWorker)