I’m dealing with a fix for an app that must run on Windows XP and Mac OS X 10.6.8. Because of this, I’ve shifted back to RS 2012r2.1. In trying to sort some Windows XP CPU use issues, I’ve switched a couple of shells to threads and now I need to update the UI. Of course, A Timer is the instant answer - except, I need to generate the timer in code. I know that I can do this, but the mechanism is giving me fits tonight.
Do any of you old timers have a code-based Timer example generating the timer instance in code and assigning the action callback you could share?
Why don’t you subclass thread, add a property like sStatus as String, pass your status through that thread property? The thread can run and update it’s property, and you can have a timer object on the window constantly checking the thread sStatus property.
It’s the other way around, but you don’t need to add handlers and stuff.
(you can change the channel of a thread after posting by clicking the channel at the top and selecting a different one)
Sorry that it got multi-posted - the server wasn’t responding and it kept returning me to edit mode.
There are lots of bits that need updating, and I may need anywhere from 1 to 10 of these which is why I don’t want to use static timers since I need to assign the timer to the thread. My idea was to generate the timer in the top of the Run event before the thread’s loop. That would keep all of the variables local to the specific thread instance.
If you take my design approach you can move the code forward and back between RealStudio and Xojo without having to worry about your number one complaint: ThreadAccessingUIException. If less friction when you need to build for older and newer OS is on your goals I would spend that time.
It’s not really the cleanest implementation of getting status updates out of a thread, but it’s the easiest.
That’s all I’ve got for now. My timer AddHandler code is buried somewhere (and I’m headed to bed, it’s 2am).
It might be in the docs though?
There are at least 3 ways to do timer in code and 2 have been mentioned above.
subclassing Timer -Most straightforward but then the specific code is not encapsulated in one class
AddHandler -It does allow keeping teh important code in one place but that is a more recent addition to the language… I think it was in RB2012
Subclass timer in a generic way, so that you store a reference to teh object with the code you want executed and a use a Class interface to call that code in the timer action event in teh timer subclass… This was the best to to keep all the relevant code in one place before AddHandler was added to the language
BTW, I just reinstalled RS2012 to check, and indeed, Timer is part of the library of controls, just like in Xojo. It does not look like a clock, though, but like an hourglass.
While I was at it, I also checked the addhandler method works just as I indicated.
I found the “oddity” that I’d created - it was in my array handling for the threads, not in the timer as I suspected. The instantiation with the add/removehandler is definitely my solution.