question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Control Binding should use Invoke

See original GitHub issue

Is 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:open
  • Created 8 months ago
  • Comments:18 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
KlausLoeffelmanncommented, Feb 7, 2023

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!

1reaction
KlausLoeffelmanncommented, Jan 30, 2023

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Under what conditions is it necessary to invoke a method?
1 Answer 1 ... As far as when Invoke() is required, or when its asynchronous equivalent BeginInvoke() is required: you need to use...
Read more >
Control.Invoke Method (System.Windows.Forms)
The Invoke method searches up the control's parent chain until it finds a control or form that has a window handle if the...
Read more >
Introduce InvokeAsync on Control · Issue #4631
Invoke is to be used when the caller wants a message to propagate immediately, and will block until it does so—the concern is...
Read more >
30 Using Command Components to Invoke Functionality in ...
This chapter describes how to invoke custom methods on ADF Business Components using ADF Faces command components and ADF Model data bindings in...
Read more >
Layouts and binding expressions
The expression language allows you to write expressions that handle events dispatched by the views. The Data Binding Library automatically ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found