`

调用线程无法访问此对象,因为另一个线程拥有该对象(转)

 
阅读更多
WPF:Dispatcher.Invoke 方法,只有在其上创建 Dispatcher 的线程才可以直接访问DispatcherObject。若要从不同于在其上创建 DispatcherObject 的线程的某个线程访问 DispatcherObject,请对与 DispatcherObject 关联的 Dispatcher 调用 Invoke 或 BeginInvoke。需要强制线程安全的 DispatcherObject 的子类可以通过对所有公共方法调用 VerifyAccess 来强制线程安全。这样可以保证调用线程是在其上创建 DispatcherObject 的线程。
  代码:
  this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
     this.lbl.Dispatcher.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));
Winfrom:Control.Invoke 方法 (Delegate),在拥有此控件的基础窗口句柄的线程上执行指定的委托。
  代码:
          this.lbl.Invoke(new Action(()=>{ this.lbl.Text = "this is a test!!!"; }));  

public void theout(object source, System.Timers.ElapsedEventArgs e)  
     {  
         this.gcc1.Dispatcher.Invoke(  
            new Action(  
                 delegate  
                 {  
                     if (this.gcc1.Offset < 1)  
                     {  
                         this.gcc1.Offset += 0.1;  
                     }  
                     else  
                     {  
                         this.gcc1.Offset = 0;  
                     }  
                 }  
            )  
      );  
         this.gcc2.Dispatcher.Invoke(  
                new Action(  
                     delegate  
                     {  
                         if (this.gcc2.Offset < 1)  
                         {  
                             this.gcc2.Offset += 0.1;  
                         }  
                         else  
                         {  
                             this.gcc2.Offset = 0;  
                         }  
                     }  
                )  
          );   
     }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics