Thread Safe, Silverlight


I use very often RIA WCF Services and I inject the same context in several ViewModel. My problem is that as you know, the context of RIA Services, is not thread safe.

So I my solution "home made" for synchronization. I use backgrounds workers, and using PostSharp, I apply my attribute [UniqueThread ("Data")] on the method and voila.

Do I complicate things? Are there simpler solutions?

Best regards,
Vincent BOUZON

In our case we added an OnUiThread method to our BaseViewModel (which also supplies INotifypropertyChanged handler and some other handy util methods).

Whenever we need to ensure an operation is done on the UI thread we call OnUiThread with a lambda expression (or a callback) to do the work.

protected delegate void OnUiThreadDelegate();

protected void OnUiThread(OnUiThreadDelegate onUiThreadDelegate)
    if (Deployment.Current.Dispatcher.CheckAccess())

An example of a call might look like:

this.OnUiThread(() =>
        this.ViewModelList = resultList;