当前位置:首 页 > 攻城湿 >Silverlight > 查看文章

While working with Silverlight and MS CRM 2011 SOAP Endpoint , you might see this exception on the EndExecute function:

Invalid cross-thread access

The cause of this is related to doing the async call from a different thread than the Silverlight UI is running on, so while the call would still go through properly, the exception would hit when the Viewmodel would try to update the UI bound to its properties.

This happens many times when we try to update UI elements from a different thread in Silverlight( asynchronous callbacks), we will run into invalid cross-thread access error if a “Dispatcher.BeginInvoke” is not used.

i.e., The invalid cross-thread access error occurs from trying to update the ui from a non ui thread. Dispatcher.BeginInvoke will get you on the right thread.

Take a look at this method here on msdn.

The resolution to this is to add the dispatcher.invoke call to the Model on the async Callback.

this.Dispatcher.BeginInvoke(delegate()
            {
                //do something here
            });

However, this won’t work on static functions/methods, because for the reason, “this” can not be used in static function. In such scenarios , you could use :

System.Windows.Deployment.Current.Dispatcher.BeginInvoke(delegate()
                {
                   //do something here
                });

Hope it helps !

【原文:http://mytechlifedays.wordpress.com/2012/02/01/silverlight-invalid-cross-thread-access-exception/

这家伙很懒,什么都没写!

—— zhaorong

zhaorong
你可能也喜欢Related Posts
众说纷纭Comments
大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头
小提示:直接粘贴图片到输入框试试
努力发送中...
  • 评论最多
  • 最新评论
  • 随机文章
footer logo
未经许可请勿自行使用、转载、修改、复制、发行、出售、发表或以其它方式利用本网站之内容
Copyright © zhaorong All Rights Reserved. 滇ICP备15006105号-1