I have an MVVM application where a state change in the model layer needs to be communicated to the view and in the view that state change needs to be acted on synchronously prior to further processing in the model layer.
The specific scenario involves a COM object shared by the model and the view (as the result of building upon a terrible legacy architecture) and the model needs to perform a Marshal.ReleaseComObject on the shared COM object, but the view needs to release its reference to the COM object beforehand (otherwise the RCW will be invalidated and the native implementation behind this particular view component will violently explode).
The mechanism I'd used to implement this was databinding a dependency property on the view to a property on the model, the viewmodel layer relays this change notification. Both the model layer and the viewmodel layer implement the necessary INotifyPropertyChanged interface to support the binding. In the view the dependency property is registered with a PropertyMetadata instance that attaches a PropertyChanged handler.
The problem I am finding is that my PropertyChanged handler is not being called synchronously. When I fire the PropertyChanged event in the model it is correctly relayed up to the property that the view binds onto, but at that point the dependency property PropertyChanged event is not fired. A few lines of code later the model calls Marshal.ReleaseComObject, and inside that call the dependency property PropertyChanged event is then being called on the same (main UI) thread.
It looks to me very much like the binding is posting the dependency property PropertyChange call onto the message loop, and the call to ReleaseComObject is causing the message loop to process that message.
After a morning of research I've come across nothing that explains why this should be occuring. Setting IsAsync=false on the binding also has no effect.
I already have an acceptable workaround for the problem but I'd really like to understand what's going on here. Does anyone know what's going on here or has experience of similar behaviour?