1

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?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Neutrino
  • 8,496
  • 4
  • 57
  • 83
  • The behaviour you describe might result from the fact that the [Dispatcher](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx) invokes operations at different [priorities](http://msdn.microsoft.com/en-us/library/System.Windows.Threading.DispatcherPriority.aspx). There is a designated priority for data binding, which is lower than the "normal" priority. Hence the all binding-related operations are postponed until after "normal" processing has finished. – Clemens Jul 24 '12 at 13:58
  • But this code is singlethreaded so why would the Dispatcher be involved at all, I'm not calling it? There is simply a model layer property (on a class that implements INotifyPropertyChanged) being bound to a dependency property on the View with an property changed callback registered using property metadata. And the property changed callback is not invoked when the model class fires its PropertyChanged event. I've found no documention that makes mention of any inherent asynchronicity in this process. – Neutrino Sep 18 '12 at 20:57
  • Maybe-related: http://stackoverflow.com/questions/8309132/how-to-prevent-re-entrancy-of-wpf-event-handler-during-activex-method-call – Rutger Nijlunsing Feb 07 '13 at 21:47

0 Answers0