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/】