Thread Safe DependencyProperty Setting/Getting
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
I'm gonna say at the outset I don't pretend to know all the reasons why setting `DependencyProperty`s is not threadsafe (in other words, they always must be set on the UI thread), but I do know that it can be a gigantic pain to build responsive multithreaded UIs while dealing with this issue .This is particularly true in the MVVM pattern where we have `DependencyProperty`'s data bound to view model properties, and we would like to change those view model properties in worker threads.
I do realize one major problem is that you can't have properties that might affect layout / measure being changed by other threads during the layout pass, and using thread locking to prevent this would mean that worker threads could get blocked by UI updates, which tends to defeat the purpose of multithreading.
One possibility, however, might be to allow the `SetValue` (or more frequently binding source property updates) to succeed when triggered in a worker thread, but internally to use the dispatcher to handle the property change. _Basically do transparently what we now have to do manually._ Until the dispatcher gets to the actual change there would be a slight disconnect between what the viewmodel thinks the value is and what the UI thinks it is, but again we have to deal with this now when using the dispatcher (i.e. we don't know when the effect of property changes will actually propagate to the UI if they occur on a worker thread).
I could easily be overlooking other difficulties, as well as other possible solutions. Either way I didn't see this among any of the issues opened since the open source announcement and thought I'd raise it to for discussion.
Contributor guide
Assessment
This issue has not been assessed yet.