Timer in a CustomClass

Is it possible to add a Timer to a custom class, and have that timer started when the class is initiated?
I know how to use a Timer, but fail to see if and how I can add one to a custom class I’m writing for a DeskTop project.

Thanks in advance for your help.

Create the timer in code in the Constructor method for your class, and start it or whatever. Then each time you do:

Var  myInstance as myClass
myInstance = new myClass

your timer for that new instance will run.

You could make your class a subclass of Timer or you can add a timer property to your class, create an instance of the timer and use AddHandler / RemoveHandler to link the timer to a method in your class.

3 Likes

or if you already have your own class you can set Super to Timer at the right side
where you see Name,Super,Interfaces

add a method Constructor
Public Sub Constructor()

  Self.Period=1000
  Self.RunMode=Timer.RunModes.Multiple
  Self.Enabled = True

End Sub

use context menu (left side in Contents tree over your class name) and “Add to Class…” Event Handler Action

1 Like

If the main purpose of the class isn’t to be a type of Timer, I’d recommend against this approach. A Timer property can be added for support of the class pretty easily as per @kevin_g 's suggestion.

2 Likes

yes but we not need AddHandler / RemoveHandler
just a context menü

Thanks for the reply, I managed to get it to work, but was a little confused by the Xojo Documentation stating to use a myTimer.run, which does not work; it needs to be myTimer.Action.
The mix of API1 and API2 documentation you find when you google is still a bit annoying.

1 Like