Threading runtime in 2.0 framework - WITH DATABINDING!!!

Questions and Answers

Threading runtime in 2.0 framework - WITH DATABINDING!!!


ivj 09-07-2006, 6:59 PM
Hey everyone!

This probably isn't a .NET forum, but this is the only place where I found a decent reply to the CrossThreadException issue. (in the thread below this one).

Now I know how to work the control's invoker method to properly set a control's property from an outside thread, but my problem is a bit more complicated.

What I have is 2 classes:

GUI - this is my form.
Logic - this is where I do all of the business proccessing, and this class is 100% unaware of the GUI.

I also have a Settings class which simply contains a string Test property.

When I initialize my GUI form, I databind a textbox to the Test property.  When the user pushes a button, a new background thread is started, where some logic function is called.  If that function changes the variable Settings.Test, I get a cross-thread exception  (I guess MS programmers didn't think it was worth the effort to make databinding thread-safe). 

There's no way I can do an Invoke from here (because logic is completely unaware of GUI components).  The worst-case scenario would be for me to actually jam the GUI into the logic and do an invoke EVERY time I want to change a setting.  But that creates another problem - when changing a setting using an invoker, it's only done on the main thread, which may execute AFTER the background thread, which might be using that variable already (with the old value).  So I'd have to duplicate variables, which makes it even uglier.

My ideal solution would be to somehow force the databinding mechanism to do the invoke by itself.  I've played around with OnSettingChanged sort of events, but no luck.  I'm even considering reflection.  Any suggestions here?
Forex Lab .NET | 4X Lab .NET © Copyright 2005-2006 ASCSE LLC | Disclaimer

Powered by Community Server, by Telligent Systems