Windows Phone and thread synchronization

This will be a short and technical post about a problem I just solved using information from Stack Overflow answers. Because none of those discussions is actually responding my question I will make this note about what I found.

Creating background workers and other threads may lead into a cross thread invoke errors when dispatching property notifications.

For example Windows Phone Pivot Application is using MVC architecture. In my case I needed to communicate with a Bluetooth device and this required thread based property management.

The Bluetooth-device message was receiver, but property setter raised cross thread access exception in NotifyPropertyChanged… using a standard code like this:

 

So I needed a solution to invoke the handler in the right way.

In Stack Overflow first I found a solution for Windows Applications, but not Windows Phone applications 🙂

 

Then I needed to find a solution avoid using ISynchronizeInvoke (it’s not present in windows phone application)

The simplest solution was to directly use Deployment.Current.Dispatcher.BeginInvoke, but this I forgot because windows phone development is not my main occupation.

 

I hope this will help you as it’s helping me.

Leave a Reply

Your email address will not be published. Required fields are marked *