dotnet中一个重要组件-BackgroundWorker

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

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

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

使用方式:
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 当后台操作已完成、被取消或引发异常时发生。





 1 None.gif // 这个例子没有做什么事情,完全是看看效果而已,但同时有个大问题,我也不知道为什么,没有去除僵硬情况。
 2 None.gif namespace  BackgroundWorkerTest
 3 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 4InBlock.gif    public partial class Form1 : Form
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 6InBlock.gif        public Form1()
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 8InBlock.gif            InitializeComponent();
 9InBlock.gif            InitialzeBackgroundWorker();
10ExpandedSubBlockEnd.gif        }

11InBlock.gif
12InBlock.gif        private void InitialzeBackgroundWorker()
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14InBlock.gif            this.backgroundWorker1.DoWork+=new DoWorkEventHandler(backgroundWorker1_DoWork);
15InBlock.gif            this.backgroundWorker1.ProgressChanged+=new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
16InBlock.gif            this.backgroundWorker1.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
17ExpandedSubBlockEnd.gif        }

18InBlock.gif
19InBlock.gif
20InBlock.gif        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif            MessageBox.Show("Completly");
23ExpandedSubBlockEnd.gif        }

24InBlock.gif
25InBlock.gif        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
27InBlock.gif            this.progressBar1.Value = e.ProgressPercentage;
28ExpandedSubBlockEnd.gif        }

29InBlock.gif        private void backgroundWorker1_DoWork(object sender,DoWorkEventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            e.Result = ComputeFibonacci(this.backgroundWorker1, e);
32ExpandedSubBlockEnd.gif        }

33InBlock.gif
34InBlock.gif        private int ComputeFibonacci(object sender, DoWorkEventArgs e)
35ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
36InBlock.gif           
37InBlock.gif            for (int i = 0; i < 100000; i++)
38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
39InBlock.gif                if (this.backgroundWorker1.CancellationPending)
40ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
41InBlock.gif                    e.Cancel = true;
42InBlock.gif                    
43ExpandedSubBlockEnd.gif                }

44InBlock.gif                else
45ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
46InBlock.gif                    this.backgroundWorker1.ReportProgress(i);                   
47ExpandedSubBlockEnd.gif                }

48InBlock.gif                
49ExpandedSubBlockEnd.gif            }

50InBlock.gif            return 0;
51InBlock.gif
52ExpandedSubBlockEnd.gif        }

53InBlock.gif
54InBlock.gif
55InBlock.gif        private void button1_Click(object sender, EventArgs e)
56ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
57InBlock.gif            this.backgroundWorker1.RunWorkerAsync();
58ExpandedSubBlockEnd.gif        }

59InBlock.gif
60InBlock.gif        private void button2_Click(object sender, EventArgs e)
61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
62InBlock.gif            this.backgroundWorker1.CancelAsync();
63ExpandedSubBlockEnd.gif        }

64ExpandedSubBlockEnd.gif    }

65ExpandedBlockEnd.gif}

给出另一种使用:继承BackgroundWorker:

None.gif namespace  UploadWinClient
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Contains common functionality for the upload and download classes
InBlock.gif    
/// This class should really be marked abstract but VS doesn't like that because it can't draw it as a component then :(
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FileTransferBase : BackgroundWorker
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
public FileTransferBase()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.WorkerReportsProgress = true;
InBlock.gif            
base.WorkerSupportsCancellation = true;            
ExpandedSubBlockEnd.gif        }

InBlock.gif
protected override void Dispose(bool disposing)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.HashThread != null && this.HashThread.IsAlive)
InBlock.gif                
this.HashThread.Abort();
InBlock.gif
InBlock.gif            
base.Dispose(disposing);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(this.HashThread != null && this.HashThread.IsAlive)
InBlock.gif                
this.HashThread.Abort();
InBlock.gif            
InBlock.gif            
base.OnRunWorkerCompleted(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnDoWork(DoWorkEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 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 codedot.gif
InBlock.gif
            this.WebService.Ping();
InBlock.gif            
base.OnDoWork(e);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif}
//截取的部分代码。
InBlock.gif
//从中给出我们要override的一些方法
InBlock.gif

InBlock.gif

BackgroundWorker在长时间的webservices中特别有用。





转载于:https://www.cnblogs.com/flyingchen/archive/2006/07/05/443078.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值