Billpeng Space

技术源自生活
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

多线程操作控件C#

Posted on 2008-12-09 01:23  billpeng  阅读(309)  评论(0编辑  收藏  举报
不要在创建控件以外的线程操作控件,Net   2.0已经把这个作为异常了。可以使用Control的Invoke方法,将操作放到UI线程上。  
  一个简单的例子  
   
  private   void   Form1_Load(object   sender,   System.EventArgs   e)  
  {  
          System.Threading.Thread   tNew   =   new   System.Threading.Thread         (new           System.Threading.ThreadStart(this.Test));  
          tNew.Start();  
  }  
   
  delegate   void   SetVisibleDelegate();  
   
  private   void   SetVisible()   //控件操作  
  {  
        this.button1.Visible   =   true;  
  }  
   
  private   void   Test()  
  {  
        this.Invoke(new   SetVisibleDelegate(SetVisible));  
  }