思想决定人生,态度改变一切

成功者找方法,失败者找借口! 做事先做人;安分做人,本分做事!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
最近一直在看wse3.0,从一个例子中偶然的收获。虽然通过后台操作,从而减少用户交互时的“僵硬”体验一直是每个程序员的追求,在今天这样ajax的时代里面更加显的重要。一切为了用户,一切为了更丰富愉快的体验。本文并不是ajax相关的东东。伟大的BackgroundWorker!

BackgroundWorker 类允许您在单独的专用线程上运行操作。耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 似乎处于停止响应状态。如果您需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用 BackgroundWorker 类方便地解决问题。

您必须非常小心,确保在 DoWork 事件处理程序中不操作任何用户界面对象。而应该通过 ProgressChanged 和 RunWorkerCompleted 事件与用户界面进行通信

使用方式:
1。给组件注册事件处理方法:
复制  保存
//正式做事情的地方
backgroundWorker1.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
//任务完称时要做的,比如提示等等
backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
//任务进行时,报告进度
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

2。添加具体事件处理方法
  • DoWork  调用 RunWorkerAsync 时发生。 
  •  ProgressChanged  调用 ReportProgress 时发生。 
  •  RunWorkerCompleted  当后台操作已完成、被取消或引发异常时发生。

复制  保存
//这个例子没有做什么事情,完全是看看效果而已,但同时有个大问题,我也不知道为什么,没有去除僵硬情况。
namespace BackgroundWorkerTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitialzeBackgroundWorker();
}
private void InitialzeBackgroundWorker()
{
this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
this.backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Completly");
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeFibonacci(this.backgroundWorker1, e);
}
private int ComputeFibonacci(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100000; i++)
{
if (this.backgroundWorker1.CancellationPending)
{
e.Cancel = true;
}
else
{
this.backgroundWorker1.ReportProgress(i);
}
}
return 0;
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
}
}
}


给出另一种使用:继承BackgroundWorker:
复制  保存
namespace UploadWinClient
{
/// <summary>
    /// Contains common functionality for the upload and download classes
    /// This class should really be marked abstract but VS doesn't like that because it can't draw it as a component then :(
    /// </summary>
    public class FileTransferBase : BackgroundWorker
{
public FileTransferBase()
{
base.WorkerReportsProgress = true;
base.WorkerSupportsCancellation = true;
}
protected override void Dispose(bool disposing)
{
if (this.HashThread != null && this.HashThread.IsAlive)
this.HashThread.Abort();
base.Dispose(disposing);
}
protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
{
if (this.HashThread != null && this.HashThread.IsAlive)
this.HashThread.Abort();
base.OnRunWorkerCompleted(e);
}
protected override void OnDoWork(DoWorkEventArgs e)
{
// make sure we can connect to the web service.  if this step is not done here, it will retry 50 times because of the retry code
            this.WebService.Ping();
base.OnDoWork(e);
}
}
//截取的部分代码。
    //从中给出我们要override的一些方法
}
 
原文出处:http://www.chenjiliang.com/Article/View.aspx?ArticleID=2032&TypeID=98
posted on 2008-01-13 21:19  投石问路  阅读(310)  评论(0编辑  收藏  举报