微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Silverlight 跨线程访问无效

解决办法有以下几种1使用 SynchronizationContext;此方法需要注意的是,System.Threading.SynchronizationContext.Current必须在UI线程中调用,如果在子线程中,将返回null;

@H_404_12@
双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
private void button1_Click( object sender,RoutedEventArgs e)  
{  
     System.Threading.SynchronizationContext sc = System.Threading.SynchronizationContext.Current;  
     System.Threading.Thread thread= new System.Threading.Thread(()=>{  
         sc.Post((o) => {  
             ((Button)sender).Content = "Hello" ;  
         }, null );  
              
       
     });  
     thread.Start();  
}

2 使用dispatcher.BeginInvoke方法

@H_404_12@
双击代码全选
1
2
3
4
5
6
7
8
9
10
private void button1_Click( object sender,RoutedEventArgs e)  
{  
          
     System.Threading.Thread thread= new System.Threading.Thread(()=>{  
       
         this .dispatcher.BeginInvoke(() => { ((Button)sender).Content = "Hello" ; });  
               
     });  
     thread.Start();  
}

3 使用BackgroundWorker 类来代替你的线程类

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐