代码改变世界

BackGroundWorker学习

2010-03-01 20:50  爱研究源码的javaer  阅读(291)  评论(0编辑  收藏  举报
代码
 public partial class FrmBackGroundWorker : Form
    {
        
public FrmBackGroundWorker()
        {
            InitializeComponent();
        }

        
private void startBtn_Click(object sender, EventArgs e)
        {
            
this.backgroundWorker1.RunWorkerAsync(2000);
        }

        
private void cancelBtn_Click(object sender, EventArgs e)
        {
            
this.backgroundWorker1.CancelAsync();
        }

        
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw 
= sender as BackgroundWorker;

            
int arg = (int)e.Argument;

            e.Result 
= TimeConsumingOperation(bw, arg);

            
if (bw.CancellationPending)
            {
                e.Cancel 
= true;
            }
        }

        
private int TimeConsumingOperation(BackgroundWorker bw, int sleepPeriod)
        {
            
int result = 0;

            Random rand 
= new Random();

            
while (!this.backgroundWorker1.CancellationPending)
            {

                
bool exit = false;

                
switch (rand.Next(3))
                {
                    
case 0:
                        {
                            
//result = 1;

                            
//exit = true;

                            
//break;

                            
throw new Exception("An   error   condition   occurred.");
                        }
                    
case 1:
                        System.Threading.Thread.Sleep(sleepPeriod);
                        exit 
= true;
                        
break;
                    
case 2:
                        {
                            result 
= 23;
                            exit 
= true;
                            
break;
                        }
                    
default:
                        {
                
                            
break; }
                
                }
                
if (exit)
                {
                    bw.ReportProgress(
100100);
                    
break; }
            
            
            }

            
return result;
        
        
        }

        
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            
if (e.Cancelled)
            {
                MessageBox.Show(
"");

            }
            
else if (e.Error != null)
            {
                
string msg = String.Format("as {0}", e.Error.Message);

                MessageBox.Show(
"");

            }
            
else
            {
                
string msg = String.Format("Result   =   {0} ", e.Result);
                MessageBox.Show(msg); 

            }
        }

        
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
//this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);

            
this.progressBar1.Value = Math.Min(this.progressBar1.Maximum, e.ProgressPercentage);
        }
    }

 http://www.cnblogs.com/dlwang2002/archive/2006/12/07/585093.html

http://www.cnblogs.com/idior/archive/2005/07/25/199946.html .net新线程

http://www.cnblogs.com/yizhu2000/archive/2007/10/19/929930.html多线程总结

http://www.cnblogs.com/happy555/archive/2007/11/07/952315.html BackGroundWorker使用经验

http://www.cnblogs.com/artech/archive/2008/07/30/1256144.html 一个BackGroundWorker实现WinForm异步操作的例子