使用BackgroundWorker控件进行异步处理

BackgroundWorker是vs2005自带的,以下代码实现异步修改TextBox的Text

 1   private   void  button2_Click( object  sender, EventArgs e)
 2          {
 3            this.textBox1.Text = "start";
 4
 5            this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
 6            this.backgroundWorker1.RunWorkerAsync();            
 7        }
      
 8
 9          private   void  backgroundWorker1_DoWork( object  sender, DoWorkEventArgs e)
10          {
11            for (int i = 0; i < 3; i++)
12            {
13                for (int j = 0; j < 100000000; j++)
14                {
15                    j++;
16                    j--;
17                }

18                this.textBox1.Text = i.ToString();
19            }

20        }

21
22          private   void  backgroundWorker1_RunWorkerCompleted( object  sender, RunWorkerCompletedEventArgs e)
23          {
24            this.textBox1.Text = "complete";
25        }


 

你可能感兴趣的:(background)