BackgroundWorker 类允许您在单独的专用线程上运行操作。耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 似乎处于停止响应状态。如果您需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用 BackgroundWorker 类方便地解决问题。
若要在后台执行耗时的操作,请创建一个 BackgroundWorker,侦听那些报告操作进度并在操作完成时发出信号的事件。可以通过编程方式创建 BackgroundWorker,也可以将它从“工具箱”的“组件”选项卡中拖到窗体上。如果在 Windows 窗体设计器中创建 BackgroundWorker,则它会出现在组件栏中,而且它的属性会显示在“属性”窗口中。
若要设置后台操作,请为 DoWork 事件添加一个事件处理程序。在此事件处理程序中调用耗时的操作。若要启动该操作,请调用 RunWorkerAsync。若要收到进度更新通知,请对 ProgressChanged 事件进行处理。若要在操作完成时收到通知,请对 RunWorkerCompleted 事件进行处理。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { private int numberToCompute = 0; private int highestPercentageReached = 0; public Form1() { InitializeComponent(); InitializeBackgoundWorker(); } private void InitializeBackgoundWorker() { //添加委托 backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); //委托完成后执行的事件 backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); //执行委托过程中报告进度信息 backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); } private void Form1_Load(object sender, EventArgs e) { } //开始异步执行 private void startAsyncButton_Click_1(object sender, EventArgs e) { // Reset the text in the result label. resultLabel.Text = String.Empty; // Disable the UpDown control until // the asynchronous operation is done. this.numericUpDown1.Enabled = false; // Disable the Start button until // the asynchronous operation is done. this.startAsyncButton.Enabled = false; // Enable the Cancel button while // the asynchronous operation runs. this.cancelAsyncButton.Enabled = true; // Get the value from the UpDown control. numberToCompute = (int)numericUpDown1.Value; // Reset the variable for percentage tracking. highestPercentageReached = 0; // 使用RunWorkerAsync来激活backgroundWorker1执行 backgroundWorker1.RunWorkerAsync(numberToCompute); } private void cancelAsyncButton_Click(object sender, EventArgs e) { // Cancel the asynchronous operation. this.backgroundWorker1.CancelAsync(); // Disable the Cancel button. cancelAsyncButton.Enabled = false; } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //e.Argument接收RunWorkerAsync传递过来的numberToCompute e.Result = ComputeFibonacci((int)e.Argument, worker, e); } // This event handler deals with the results of the // background operation. private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // First, handle the case where an exception was thrown. if (e.Error != null) { MessageBox.Show(e.Error.Message); } else if (e.Cancelled) { // Next, handle the case where the user canceled // the operation. // Note that due to a race condition in // the DoWork event handler, the Cancelled // flag may not have been set, even though // CancelAsync was called. resultLabel.Text = "Canceled"; } else { // Finally, handle the case where the operation // succeeded. resultLabel.Text = e.Result.ToString(); } // Enable the UpDown control. this.numericUpDown1.Enabled = true; // Enable the Start button. startAsyncButton.Enabled = true; // Disable the Cancel button. cancelAsyncButton.Enabled = false; } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.progressBar1.Value = e.ProgressPercentage; } long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e) { if ((n < 0) || (n > 91)) { throw new ArgumentException("value must be >= 0 and <= 91", "n"); } long result = 0; if (worker.CancellationPending) { e.Cancel = true; } else { if (n < 2) { result = 1; } else { result = ComputeFibonacci(n - 1, worker, e) + ComputeFibonacci(n - 2, worker, e); } // Report progress as a percentage of the total task. int percentComplete = (int)((float)n / (float)numberToCompute * 100); if (percentComplete > highestPercentageReached) { highestPercentageReached = percentComplete; worker.ReportProgress(percentComplete); } } return result; } } }
另外一个例子:一个基于BackgorundWorker的例子。由于这个理基本上实现了BackgorundWorker的大部分功能:异步操作的启动、操作结束后的回调、异步操作的撤销和进度报告等等有两组相互独立的数据需要逐条获取和显示,左边和右边两个groupbox分别代表基于这两组数据的操作,由于他们完全独立,因此可以并行执行。当点击Start按钮,以异步的方式从存储介质中逐条获取数据,并将获取的数据追加到对应的ListBox中,ProgressBar真实反映以获取的数据条数和总记录条数的百分比,同时,当前获取的条数也会在下方的Label上随着操作的继续而动态变化。此外通过点击Stop按钮,可以中止掉当前的操作。当操作被中止后,ProgressBar和Label反映中止的那一刻的状态。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsFormsApplication2 { public partial class Form2 : Form { private int ppo = -1; public Form2() { InitializeComponent(); InitializeBackgroundWork(); } private void InitializeBackgroundWork() { //添加委托 backgroundWorker1.DoWork+=new DoWorkEventHandler( backgroundWorker1_DoWork); //委托完成后要执行的事件 backgroundWorker1.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); //进度报告事件 backgroundWorker1.ProgressChanged+=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); } private void backgroundWorker1_DoWork(object oj,DoWorkEventArgs e) { try { ReadData(backgroundWorker1,e); } catch(Exception EX) { } } private void ReadData(BackgroundWorker work ,DoWorkEventArgs e) { for (int i = 0; i < 100; i++) { if (work.CancellationPending) return; work.ReportProgress(i,new KeyValuePair<int, string>(i,Guid.NewGuid().ToString() )); Thread.Sleep(1000); } } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { KeyValuePair<int,string> kp=(KeyValuePair<int,string>)e.UserState; if (kp.Key == ppo) return; else ppo = kp.Key; listBox1.Items.Add(kp.Value); label1.Text = string.Format("There is have {0} data", kp.Key); progressBar1.Value = e.ProgressPercentage; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { label1.Text = string.Format(label1.Text+" is over!!!"); this.end.Enabled = false; this.start.Enabled = true; } private void start_Click(object sender, EventArgs e) { this.start.Enabled = false; this.end.Enabled = true; this.label1.Text = ""; this.listBox1.Items.Clear(); backgroundWorker1.RunWorkerAsync(); } private void end_Click(object sender, EventArgs e) { backgroundWorker1.CancelAsync(); this.end.Enabled = false; this.start.Enabled = true; } } }