views:

131

answers:

4

In WPF we have two threads (at least): rendering and a UI thread. When I raise an event OnNotifyPropertyChanged on some property changes, it is raised on the UI thread. This information needs to be dispatched to WPF rendering thread for re-rendering. I am assuming it is done in a synchronous manner ( Dispatcher.Invoke ) but how does it really work?

If I raise multiple OnNotifyPropertyChanged events for the same data structure without locking access to the accessor property for this data structure for which these events have been raised, am I creating a potential race condition? I have seen the infamous "Collection was modified; enumeration operation may not execute" exception coming from WPF, so it looks like WPF processes these events asynchronously. Am I misunderstanding the exception? Thanks!

A: 

Hi

My thoughts

1- If you are accessing a state but NOT modyfing than you are inherently thread safe.

2- Collection was Modified will be raised when you are programitacally adding/removing an item in a for or foreach loop, there is no relation to the INotifyPropertyChanged events.

saurabh
what I am seeing is that foreach loop is executed in .NET framework code, after I notify that the property has changed, so that's why I think INotifyPropertyChanged is relevant.
Lenik
A: 

Hope when you are refering two threads, you are refering

  1. Rendering thread
  2. UI Thread.

Yes you are right the update is a ASYNCH

Take a look at http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

Guru
Guru, where does it say that WPF processes NotifyPropertyChanged event asynchronously?
Lenik
+1  A: 

The exception "Collection was modified; enumeration operation may not execute" is not related to WPF, it is raised from IEnumerator when you iterated on a collection with foreach and while doing that the collection is somehow changed (add/remove/modify). (e.g: http://social.msdn.microsoft.com/forums/en/netfxbcl/thread/7ce02724-2813-4f7d-8f3c-b1e3c1fd3019/) .

Other than that I have never encountered exception caused by multiple simultaneous invokes on PropertyChanged event.

Captain
the point is that the collection is iterated by WPF and not by my code. When I turn first chance exceptions on, I see it fail in .NET
Lenik
What thread is the exception thrown on?
Caleb Vear
My best guess is that it's happening because you modify the collection during the load of the xaml. The binding is trying to iterate on the collection to populate it on your ItemsControl, and at the same time you're modifying it. Try to sync it.
Captain
A: 

Are you doing any processing your self on the non UI thread? I am pretty certain that the iteration of any enumerations you are binding to will be done on the UI thread, so if after you raise the event someone else in your application modifies the collection you would get this exception.

The problem should not be being caused by the rendering thread iterating over your collection as it doesn't ever do that.

Caleb Vear