委托操作控件使用01

protected delegate void UpdateControlText(string strText); //定义一个委托  
 
       //定义更新控件的方法  
       protected void updateControlText(string strText)
       {
           this.label1.Text = strText;
           return;
       }  
       private void button1_Click(object sender, EventArgs e)
       {
           Thread ff = new Thread(new ThreadStart(x1));
           ff.Start();  
       }
       private void x1()//线程安全的访问窗体控件  
       {
           for (int i = 0; i < 1000; i++)
           {
               long xx = Convert.ToInt32(this.label1.Text);
               if (this.InvokeRequired)
               {
                   //用更新控件的方法updateControlText实例化一个委托update  
                   UpdateControlText update = new UpdateControlText(updateControlText);
                   this.Invoke(update, Convert.ToString(++xx));  //调用窗体Invoke方法  
               }
               else
               {
                   this.label1.Text = Convert.ToString(++xx);
               }
           }
       }

posted on 2017-07-13 15:19  bobob  阅读(144)  评论(0编辑  收藏  举报

导航