Tips   >   Externals   >   Timer Object

Timer Object

The first time I tried to use the timer object it took a while to find because it wasn't loaded. Review the topic on Loading External Components if the Timer object doesn't show up in your external components treelist.

To use the timer object you first create an object class which has the timer object as its superclass.

  1. Create a new object class. e.g. oCustomTimer
  2. With the object class selected, press F6 to open the Property Manager.
  3. Click the $superclass property droplist button.
  4. Scroll down the treelist to the External Objects node and select the Timer child node.

Once the timer object class is created, you can instantiate it using an object data type variable. Depending on the purpose you can use a task variable, class variable, or instance variable to instantiate your object class which has the timer object as its superclass.

  1. Go to the variables pane in the method editor of the class where you wish to instantiate the timer object subclass.
  2. Add a new variable. eg. instance variable ioTimerObj
  3. Set the Type to Object.
  4. Click the droplist of the variable's Subtype.
  5. Navigate to the timer object class which you created in your library.

Your Timer object class will have several properties and methods which are reviewed below.

Click the Run Demo button to open a window which demonstrates an oTimerDemo object class and a technique for adding an observer to a timer object. The observer is notified each time a $timer message is received by the timer object.

$autoreset

The timer object $autoreset property can be set to kTrue or kFalse.

If set to kFalse the timer object will send a $timer message once and then stop. If set to kTrue the timer object will continue to send $timer messages at the interval specified by the $timervalue and $useseconds properties until it is sent a $stoptimer message.

$reentrant

The timer object $reentrant property can be set to kTrue or kFalse.

If set to kTrue the timer sends a $timer message and immediately restarts the timer without waiting for the $timer method to execute.

If set to kFalse the timer sends a $timer message and waits for the $timer method to complete before restarting the timer.

$resettimer

Sending a $resettimer message to the timer object stops and then restarts the timer using the current value of $timervalue.

$starttimer

Sending a $starttimer message to the timer object starts the timer using the current value of $timervalue.

$stoptimer

Sending a $starttimer message to the timer object stops the timer.

$timer

When the timer counts down to zero a $timer message to the timer object. The pEventCode is evTimer. You override the $timer method of your timer object subclass and enter the code you want executed when the timer counts down to zero.

$timervalue

The timer object $timervalue property can be set to any integer value. The unit of measure will either be seconds or milliseconds depending on the $useseconds property setting.

$useseconds

The timer object $useseconds property can be set to kTrue or kFalse.

If set to kTrue the unit of measure for $timervalue will be seconds. If set to kFalse the unit of measure will be milliseconds.

Timer Crashes Omnis

Some Omnis developers have reported that a timer object initiated method will crash Omnis Studio if the timer object initiated method kicks in when another Omnis Studio method is executing.

To prevent this from happening you can use the sys(90) function to check the number of methods in the method stack and abort running the timer object method if other methods are running at the same time.

Place the following code at the start of your $timer method:

; Check the number of methods currently in the method stack.
Calculate MethodStackLineCount as sys(90)
If MethodStackLineCount<>1
   
   ; A method other than this one is currently running.
   ; Restart the timer object if it is not $reentrant.
   If $cinst.$reentrant=kFalse
      Do $cinst.$starttimer()
   End If
   Calculate FlagOK as kTrue
   
Else
   
   ; No other methods are in the method stack.
   ; Run the timer object method initiated code.
   Breakpoint
   
End If
Quit method FlagOK

Note

The sys(9) function does not work for web client requests running in a thread of the multi-threaded server.