Thread Safe Class for accessing UI Objects.

I have created a thread safe class for accessing UI Objects properties,methods, and calling a msgbox without having to create a timer object independently of the thread object. You can download the first go at it here code is not documented but should still be easy to understand.

Here is some example code of how you can access objects within a thread:

[code] dim d as new date
//Change the value of a UI objects property
ThreadSafeUIChange PushButton1,“Caption”,d.LongTime
//Read the value of UI objects Property
//Display a msgbox
ThreadSafeMsgBox “Message Text”,me
//Call a method of a UI object and pas some parameters
dim prams as new JSONItem
prams.Append “Some values”

  ThreadSafeMethod window1,"msgtest",prams

///Calll a method of a UI object with no parameters
ThreadSafeMethod me,“kill”

I’ll be honest, I came here looking to pick the code apart because it sounds like a very bad idea, but I can’t find anything actually frightening about it.

I would recommend using App.YieldToNextThread instead of App.DoEvents however, just to make it more clear to anybody looking at the code what exactly is going on. I know App.DoEvents will yield when called from a thread anyway, but this just makes it more clear. Also you might want to consider detecting if the current thread is the main thread using and getting/setting the value more directly, rather than waiting on the timer, since it would be allowed. This is just in case somebody calls one of the ThreadSafe methods on the main thread. Again, your code will work as it is, but this will save a little bit of performance just in case.

To anybody reading this thread that hasn’t looked at the code, it looks like it’ll work just fine, though it uses a whole lot of introspection which will be a performance hit. I think aside from the introspection part of it, this same thing was done by the framework during one of the beta periods, and performance was so bad people demanded the behavior be turned off. So just be aware. Only you can decide if that hit is worth it.

Made a small updated doing what Thom had suggested above. I also added support to call a delegate method which you can pass a dictionary that holds your parameter values. Using a delegate method means the no introspection is needed so you don’t take a performance hit. You can call the delegate as either blocking or non blocking. If you call the delegate method as blocking when it executes it will suspend the thread you called it from until it is complete.