RaiseCanExecuteChanged on non UI thread

Jan 31, 2012 at 9:01 AM

Hi,

when working with DelegateCommand in my WPF project I ran into an InvalidOperationException ("The calling thread cannot access this object because a different thread owns it.") in a call to RaiseCanExecuteChanged.

Maybe calls to RaiseCanExecuteChanged are not routed to the UI thread automatically (as it is done calling NotifyPropertyChanged)?

Thanks
Peter

Coordinator
Jan 31, 2012 at 3:52 PM

You're going to need to marshall the call yourself for commands.  The reason is that methods wired to command may not necessarily touch UI elements.  What you need to do is handle it in the method being invoked via the command.  The way I like to do this is use a SynchronizationContext.

Cheers,

Tony

Feb 1, 2012 at 10:38 AM

Thank you for the quick response. I will handle this as suggested.

Kind regards
Peter