6

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!

Lenik
  • 1,518
  • 1
  • 16
  • 24

3 Answers3

1

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
  • 713
  • 1
  • 7
  • 20
  • 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 Sep 08 '10 at 18:35
  • 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 Sep 12 '10 at 12:23
0

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
  • 181
  • 1
  • 6
0

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
  • 2,637
  • 2
  • 21
  • 20