ApplicationSettings bound controls don't refresh when a web interface changes setting

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
Got a problem here that's driving me nuts

I have a program that opens a windows form and also starts an HTTP listener. Let us suppose there is one textbox on the form, whose text property is bound to the application settings "MyText"

If I programmatically change the setting directly using a timer on the form, the textbox updates to show the new value
If I programmatically change the setting from the web interface/httplistener handler code the textbox doesnt update, though when the new web page is generated it can see the changed property (so the property is changing correctly)

No exceptions are being thrown that could upset things (I run with Break-on-thrown-exceptions enabled for all exception types) and I'm currently at a loss to explain this behaviour discrepancy. The only wondering I currently have is if it's to do with cross threading; the httplistener is async and will hence run in its own completer thread.. Perhaps updating the properties from that thread will mean no event is raised and dealt with by the UI thread?
 
The httpl doesnt update the textbox directly, it updates the My.Settings value, which I presumed the textbox would then see.. I've jsut got it working now by attaching an eventhandler to the PropertyChanged event of the settings, and then in tha thandler, checking if an Invoke is required and performing it if necessary.. As it turns out I believe that is indeed the problem: if you update My.Settings propertty values from anything other than a control's UI thread, the control will not refresh to the new bound value..

The full solution described (I cant really post code because it's c#), on my form after init, I loop every control and check if it has databindings, then store the bound property name and the binding in a Dictionary(Of string, Binding). When the event is raised I first check if the property name of the setting that changed, has any binding. If it does, I do a

VB.NET:
If InvokeRequired Then
  Invoke(new Action(Of Object, PropertyChangedEventArgs)(name_of_my_event_handler), sender, e))
  Return
End If

This uses the Action(T,T) delegate and invokes the same thing that was just called, on the UI thread. There may be a cleaner way, but I couldnt think of it
 
Back
Top