WPF 数据绑定线程安全

发布于 2024-12-03 21:05:43 字数 259 浏览 0 评论 0原文

我有一个 TextBox,它绑定到一个属性,该属性在后台线程中以非常快的速度进行修改。 WPF 线程中的数据绑定安全吗?属性或文本框中的数据是否会不同步?是否有必要(甚至可能)对参与数据绑定的属性使用同步?

据我了解,如果属性所在的类实现 INotifyPropertyChanged,则绑定框架会自动将 UI 更新编组到 UI 线程。但是,这不是仍然会导致数据不同步吗?如果我理解正确,从一个线程写入并从另一个线程读取的变量应该通过同步...数据绑定是异常吗?

谢谢!!

I have a TextBox that's bound to a property that gets modified at a very rapid rate in a background thread. Is data binding in WPF thread safe? Will the data in the property or the TextBox ever get out of sync? Is it necessary (or even possible) to use synchronization on a property that takes part in data binding?

I understand that, if the class on which the property resides implements INotifyPropertyChanged, the binding framework automatically marshalls the UI update to the UI thread. However, doesn't that still allow the data to get out of sync? If I understand correctly, variables that are written from one thread and read from another should by synchronized... is data binding an exception?

thanks!!

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

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

发布评论

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

评论(2

离去的眼神 2024-12-10 21:05:43

是的,在大多数情况下。绑定对于单个对象来说是线程安全的(因此对于您的字符串来说应该没问题)。但是,绑定到集合不是线程安全 - 并且仍然需要手动封送。如果您有一个绑定到集合的控件,则无法在后台线程上更改该集合。

据我所知,如果属性所在的类实现了 INotifyPropertyChanged,则绑定框架会自动将 UI 更新编组到 UI 线程。但是,这是否仍然会导致数据不同步?

这不应该失去同步,除非多个线程非常快速地写入变量(在这种情况下,它们都会阻塞,直到它们恢复同步,但是有一段时间线程将“等待”用户界面)。编组是同步发生的,因此线程在绑定更新之前不会接收值。这可能会减慢您的处理速度,因为 UI 更新必须在后台线程继续之前进行。

Yes, for the most part. Binding is thread safe for single objects (so should be fine for your string). However, binding to a collection is not thread safe - and still requires manually marshaling. If you have a control bound to a collection, you cannot change the collection on a background thread.

I understand that, if the class on which the property resides implements INotifyPropertyChanged, the binding framework automatically marshalls the UI update to the UI thread. However, doesn't that still allow the data to get out of sync?

This shouldn't get out of sync, unless multiple threads are writing to the variable very quickly (in which case, they'll all block until they get back in sync, but there is a period of time where threads will "wait" on the UI). The marshaling happens synchronously, so the thread doesn't receive values until the binding is up to date. This can slow down your processing, as the UI update has to happen before your background thread can continue.

看春风乍起 2024-12-10 21:05:43

是的,它通常是线程安全的。在 WPF 中(与 WinForms 不同),数据绑定类查找 UI 线程的 Dispatcher 并使用它(如果需要)自动编组到 UI 线程。但请注意,这是同步完成的 - 当 UI 重绘时,您的后台线程将被阻塞,我已经看到这会导致不稳定、“冻结”和其他快速后台更新的意外效果。

请在此处查看类似的问题:WPF 数据绑定线程安全性?

Yes, it is usually thread safe. In WPF (unlike WinForms) the data binding classes look for the UI thread's Dispatcher and use it (if needed) to automatically marshal to the UI thread. Note, however, that this is done synchronously- your background thread will block while the UI is redrawn, and I have seen that cause choppyness, "freezing", and other unintended effects with rapid background updates.

See a similar question here: WPF Databinding thread safety?

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