GetProperties - why in Thread? Threads problem

Grids for WPF Forum

Posted 15 years ago by Aleksander Brzozowski
Version: 4.5.0485
I have own class which implements ICustomTypeDescriptor interface.
I`ve noticed that function GetProperties(Attribute[] attrbiutes) is called in Thread.
I dont know why many times (for example: when first time set the SelectedObject property in PropertyGrid is called 1, second time 2, next time 4, next 5...etc. In addition in different threads - sometimes in main Thread)
It causes error (calling thread cannot access to other thread) when I raise NotifyPropertyChanged event, because I have property editor which is UIElement and he is binded to my object.
Here is example, what am I doing
I have object with many properties, when I change one property it causes automatic change in other property.

 IPropertyDataAccessor accessor = VisualTreeHelperExtended.GetAncestor(channelsContainer, typeof(IPropertyDataAccessor)) as IPropertyDataAccessor;
accessor.Value = myList; - I set my first property, but it causes that in my object I must refresh other property - so I call NotifyPropertyChanged

myObject.NotifyPropertyChanged("MySecondProperty"); (NotifyPropertyChanged is my method when I raise NotifyPropertyChangedHandler)

Sometimes (when GetProperties was called in thread) I get error that calling thread cannot access to other thread)

Is bug, or I try do something wrong?

[Modified at 03/17/2009 02:14 AM]

Comments (6)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Aleksander,

The PropertyGrid is capable of retrieving properties asynchronously, which it does by default. You would need to set PropertyGrid.IsAsynchronous to false to disable this feature. Disabling this means that the PropertyGrid will perform all property retrieval using the main UI thread.

On a side, the GetProperties method should not be altering the state of the object. So I'm not sure I understand why the property changed notification is raised.

Actipro Software Support

Posted 15 years ago by Aleksander Brzozowski
Thanks for reply,

GetProperties does`nt change state of the object, property changed notification is raised in other place.
IsAsynchronous property...I overlooked this...I`ll try to do my stuff in other way.

Posted 14 years ago by Cheetah
I'm having what I think is the same problem, with version 9.2.0515, some more details that might be helpful:

I have an object displayed in the property grid, that object exposes property change notifications.

A background thread (frequently) makes a change to the object, and this triggers a property change notification.

The property changed event handler that the property grid bound to my object throws an exception:

InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

Stack Trace:
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.Threading.DispatcherObject.VerifyAccess()
   at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
   at System.Windows.FrameworkElement.get_LoadedPending()
   at System.Windows.FrameworkElement.get_IsLoaded()
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyGridDataAccessorItem.a()
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyGridDataAccessorItem.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e)
   at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list)
   at System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args)
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.DataAccessorBase.OnPropertyChanged(PropertyChangedEventArgs e)
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.DataAccessorBase.OnPropertyChanged(String propertyName)
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.CachedPropertyDataAccessorBase.OnValuesChanged()
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.CachedPropertyDataAccessorBase.OnValueChanged()
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyDescriptorDataAccessor.a(Object A_0, EventArgs A_1)
   at ActiproSoftware.Windows.Controls.PropertyGrid.Primitives.PropertyDescriptorDataAccessor.PropertyDescriptorChangeNotifier.a(Object A_0, EventArgs A_1)
   at ...<my code>...
I tried adding a check to my code if the target object for the event handler delegate is a DispatcherObject and in that case using Dispatcher.BeginInvoke, but the property grid uses a small wrapper/shim object to bind the property change notification, and that shim object is not a DispatcherObject, so that strategy doesn't work :(
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA

Your issue is actually different that the one above. In your case you are effectively raising a PropertyChanged event from our object on a thread other than the WPF UI thread. This is what ultimately causes the issue.

This isn't something that is supported by WPF or our PropertyGrid. You would need to raise the PropertyChanged event on the UI thread, before it gets to PropertyDescriptorDataAccessor or PropertyGridDataAccessorItem.

Actipro Software Support

Posted 14 years ago by Cheetah
How is the object that implements INotifyPropertyChanged, which is defined in an assembly that knows nothing about UI (and in fact references no UI-related assemblies) supposed to know that a) it's in a grid b) which thread is the UI thread and c) how to invoke the event handler on said thread?

What if said object were in a third party module and I couldn't modify its source code?
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA

Sorry, we understand your frustation, but this is a limitation of WPF (and Silverlight & WinForms for that matter). You can find several articles relating to this, such as this one or this one.

Actipro Software Support

The latest build of this product (v24.1.2) was released 28 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.