INotifyPropertyChanged:幕后发生了什么?

发布于 2024-09-16 23:23:28 字数 329 浏览 1 评论 0原文

在 WPF 中,我们(至少)有两个线程:渲染线程和 UI 线程。当我针对某些属性更改引发事件 OnNotifyPropertyChanged 时,它会在 UI 线程上引发。需要将此信息分派到 WPF 渲染线程进行重新渲染。我假设它是以同步方式完成的( Dispatcher.Invoke ),但它实际上是如何工作的?

如果我为同一数据结构引发多个 OnNotifyPropertyChanged 事件,而没有锁定对此已引发这些事件的数据结构的访问器属性的访问,我是否会创建潜在的竞争条件?我已经看到来自 WPF 的臭名昭著的“集合已修改;枚举操作可能无法执行”异常,因此看起来 WPF 异步处理这些事件。我是否误解了异常?谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无远思近则忧 2024-09-23 23:23:28

异常“集合已修改;枚举操作可能无法执行”与 WPF 无关,当您使用 foreach 迭代集合并且集合以某种方式更改(添加/删除/修改)时,会从 IEnumerator 引发该异常。 (例如:http:// Social.msdn.microsoft.com/forums/en/netfxbcl/thread/7ce02724-2813-4f7d-8f3c-b1e3c1fd3019/

除此之外,我从未遇到过因同时调用 PropertyChanged 事件而导致的异常。

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.

憧憬巴黎街头的黎明 2024-09-23 23:23:28

希望当您引用两个线程时,您指的是

  1. 渲染线程
  2. UI 线程。

是的,您是对的,更新是异步的,

请查看 http://msdn .microsoft.com/en-us/magazine/cc163328.aspx

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

呆头 2024-09-23 23:23:28

您是否在非 UI 线程上自行进行任何处理?我非常确定您绑定到的任何枚举的迭代都将在 UI 线程上完成,因此,如果在引发事件后应用程序中的其他人修改了集合,您将收到此异常。

该问题不应该是由渲染线程迭代您的集合引起的,因为它永远不会这样做。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文