Control Binding should use Invoke
See original GitHub issueIs your feature request related to a problem? Please describe
I have a WinForms application which uses CommunityToolkit.Mvvm for bindings. I am also using the same MVVM library to build a MAUI application for study purposes.
MVVM allows me to have asynchronous commands and messages, which I use to propagate changes across models. However, because my form binds some controls to those models, I have started to receive UI Thread exceptions when updating values.
After a lot of debugging, I saw that the SetPropValue
method in the Bindings class does not use the Invoke
method when updating values:
https://github.com/dotnet/winforms/blob/77d01dd234bc6fb2dac3b86d6c2a0b7cd61f1507/src/System.Windows.Forms/src/System/Windows/Forms/Binding.cs#L956
Describe the solution you’d like and alternatives you’ve considered
It is my understanding that the Control
property is available for the Binding
object:
https://github.com/dotnet/winforms/blob/77d01dd234bc6fb2dac3b86d6c2a0b7cd61f1507/src/System.Windows.Forms/src/System/Windows/Forms/Binding.cs#L110
In this case, I propose that the SetPropValue
method checks if the Control
property is not null and tries to update it’s value through an execution of the Control.Invoke
method, to ensure the value change occurs in the UI Thread.
Will this feature affect UI controls?
I do not think so because the underlining flow is kept as is.
Issue Analytics
- State:
- Created 8 months ago
- Comments:18 (11 by maintainers)
Top GitHub Comments
Wait for it for a little while. I am busy the next days with some other urgent tasks, but I try to get to it this week, and take a look!
That’s definitely something we should look into, but also very cautiously, as it could potentially break existing scenarios if we introduced it unconditionally. So, if we consider it (and we should), we should think of an opt-in mechanism.