标签:
实现异步处理的方法很多,经常用的有基于委托的方式,今天记录的是基于事件的异步模式。利用BackgroundWorker组件可以很轻松的实现异步处理,并且该组件还支持事件的取消、进度报告等功能。本文以计算两个数X、Y的和为例。
public MainForm() { InitializeComponent(); this.backgroundWorker1.DoWork += this.BackgroundWorker1DoWork; this.backgroundWorker1.ProgressChanged += this.BackgroundWorker1ProgressChanged; this.backgroundWorker1.RunWorkerCompleted += this.BackgroundWorker1RunWorkerCompleted; this.backgroundWorker1.WorkerReportsProgress = true; this.backgroundWorker1.WorkerSupportsCancellation = true; this.buttonCancel.Enabled = false; }
this.backgroundWorker1.RunWorkerAsync(new Tuple<int,int>(Convert.ToInt32(this.textBoxX.Text), Convert.ToInt32(this.textBoxY.Text)));
this.backgroundWorker1.CancelAsync();
void BackgroundWorker1DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(500); if (backgroundWorker1.CancellationPending) { e.Cancel = true return; } else { backgroundWorker1.ReportProgress(i * 10); } } var t = e.Argument as Tuple<int,int>; e.Result = t.Item1 + t.Item2; }
void BackgroundWorker1ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { this.progressBar1.Value = e.ProgressPercentage; }
void BackgroundWorker1RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { this.textBoxResult.Text = e.Cancelled ? "Canceled" : e.Result.ToString(); this.buttonCancel.Enabled = false; this.buttonCalculate.Enabled = true; this.progressBar1.Value = 100; }
1 /* 2 * 由SharpDevelop创建。 3 * 用户: David Huang 4 * 日期: 2015/9/8 5 * 时间: 14:54 6 */ 7 using System; 8 using System.Windows.Forms; 9 10 namespace BackgroundWorkerTest 11 { 12 /// <summary> 13 /// Description of MainForm. 14 /// </summary> 15 public partial class MainForm : Form 16 { 17 public MainForm() 18 { 19 InitializeComponent(); 20 this.backgroundWorker1.DoWork += this.BackgroundWorker1DoWork; 21 this.backgroundWorker1.ProgressChanged += this.BackgroundWorker1ProgressChanged; 22 this.backgroundWorker1.RunWorkerCompleted += this.BackgroundWorker1RunWorkerCompleted; 23 this.backgroundWorker1.WorkerReportsProgress = true; 24 this.backgroundWorker1.WorkerSupportsCancellation = true; 25 this.buttonCancel.Enabled = false; 26 } 27 void ButtonCalculateClick(object sender, EventArgs e) 28 { 29 this.buttonCalculate.Enabled = false; 30 this.textBoxResult.Text = string.Empty; 31 this.buttonCancel.Enabled = true; 32 this.backgroundWorker1.RunWorkerAsync(new Tuple<int,int>(Convert.ToInt32(this.textBoxX.Text), Convert.ToInt32(this.textBoxY.Text))); 33 } 34 void ButtonCancelClick(object sender, EventArgs e) 35 { 36 this.backgroundWorker1.CancelAsync(); 37 } 38 void BackgroundWorker1DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) 39 { 40 for (int i = 0; i < 10; i++) { 41 System.Threading.Thread.Sleep(500); 42 if (backgroundWorker1.CancellationPending) { 43 e.Cancel = true; 44 return; 45 } else { 46 backgroundWorker1.ReportProgress(i * 10); 47 } 48 } 49 var t = e.Argument as Tuple<int,int>; 50 e.Result = t.Item1 + t.Item2; 51 } 52 void BackgroundWorker1ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) 53 { 54 this.progressBar1.Value = e.ProgressPercentage; 55 } 56 void BackgroundWorker1RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) 57 { 58 this.textBoxResult.Text = e.Cancelled ? "Canceled" : e.Result.ToString(); 59 this.buttonCancel.Enabled = false; 60 this.buttonCalculate.Enabled = true; 61 this.progressBar1.Value = 100; 62 } 63 } 64 }
代码下载:点我
标签:
原文地址:http://www.cnblogs.com/David-Huang/p/4792298.html