Subclassing is a wonderful thing that once you get your head around, you will use it a lot. I remember when I couldn’t figure it out and how to implement events, etc.
So create a new class in your app. Set it’s super to Thread. Now you have a subclass of thread and you can simply add properties, methods, event definitions, etc.
So to help you understand, event definitions are things you define that are basically methods that run OUTSIDE of your class. So you raise one of these events you have defined and that allows code you put in wherever your class is used to be run specific to the needs there - make sense?
So the traditional Xojo thread has basically one event - Run. When you start the thread the run event is called.The user interface update is new but I had implemented my own a few years ago when Xojo removed the ability to update the user interface from threads.
So in my class I have added three new event definitions: Run - so that we can have your code wherever you need to place the thread run. ThreadFinished and UIUpdate.
I added two private properties in my class - both are timers. One if FinishTImer and one is UIUpdateTimer. Now these are not needed any longer with the advent of the Xojo.Timer.CallLater method, but I have also found actual timer objects seem to be more reliable. I’ve had a few issues over the years with CallLater and I am gun-shy.
I have three methods: FinishTimerAction, UIUpdateTimerAction and UpdateUI. The first two are private.
In the run event handled for my subclass I have the following code:
Sub Run() Handles Run
FinishTimer = New Timer
AddHandler FinishTimer.Action, AddressOf FinishTimerAction
FinishTimer.Mode = Timer.ModeOff
FinishTimer.Period = 0
UIUpdateTimer = New Timer
AddHandler UIUpdateTimer.Action, AddressOf UIUpdateTimerAction
UIUpdateTimer.Mode = Timer.ModeOff
UIUpdateTimer.Period = 0
FinishTimer.Mode = Timer.ModeSingle
So when the thread starts running, it sets up the timers, it then raises its Run event so your code can execute. Once that code is finished, it then triggers the finish timer.
Here’s the methods for FinishTimerAction and UIUpdateTimerAction and UpdateUI:
Private Sub FinishTimerAction(t as timer)
#Pragma Unused t
// Using in a timer to raise the event on the main thread
Private Sub UIUpdateTimerAction(t as timer)
Public Sub UpdateUI()
UIUpdateTimer.Mode = Timer.ModeSingle
So if you want to update the UI you just call UIUpdate wherever you have the thread implemented. This probably isn’t needed any longer since Xojo now gives you the ability to do this directly. So the only thing you probably need is the Finished event.
Like I say, really easy to implement. No plug-ins needed.