//Create a property to hold a Timer and create a method to handle the Timer's action. The method must have one parameter that can be optional:  
Sub MyTimerActionMethod (sender As Timer = nil)
  // Since you don't really need to access the Timer unless you want to change its period
  // or mod
  #pragma unused sender

  // Do the stuff that has to happen periodically

End Sub  


//Add something like this to your Constructor:  
Sub Constructor()
  MyTimer = new Timer
  AddHandler MyTimer.Action, AddressOf MyTimerActionMethod
  MyTimer.Period = somePeriod
  MyTimer.Mode = Timer.ModeMultiple
End Sub  


//In the class Destructor:  
Sub Destructor()
  if MyTimer <> nil then
    MyTimer.Mode = Timer.ModeOff // Just a precaution
    RemoveHandler MyTimer.Action, AddressOf MyTimerActionMethod
    MyTimer = nil
  end if
End Sub  

//If you ever need to force the Timer's Action elsewhere in your code, you can do it by calling MyTimerActionMethod() directly.  
//(You can skip the Destructor code if you use WeakAddressOf in AddHandler, but I prefer the explicit method.)

Leave a comment

Your email address will not be published. Required fields are marked *