开发者

INotifyPropertyChanged: what happens behind the scene?

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 acces开发者_如何转开发sor 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!


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜