Product Site

5.4.23.4. init

Sets up a Ticker with a specified interval. After each interval, the Ticker sends a message triggered to the specified notification target.

The target must be an object that implements the AlarmNotification interface. It must inherit from or be a subclass of the AlarmNotification class, or a Message object (as the Message class inherits from AlarmNotification). If target is a Message object, the triggered method of the Message class will respond by simply sending the specified message.

The interval can be a TimeSpan or a String object. If it is
  • a TimeSpan, it must be of a non-negative length, which specifies the interval time length.
  • a String, it must be a non-negative number which specifies the interval time length in seconds.

If specified, attachment can be an arbitrary object that will be attached to the Ticker instance, and can later be retrieved in the event handler. See method attachment .

You can use the cancel method at any time to cancel a Ticker. If cancelled, the Ticker sends message cancel to the specified notification target.

The following code uses a Ticker to display progress information during a long-running task.
Example 5.318. Ticker class — init method
.Progress~new(0.5)~monitor(.Task~new, "tenSeconds")

-- defines tasks together with their "progress" methods
::class Task

-- long-running task
::method tenSeconds unguarded
  expose m n
  n = 1000
  do m = 1 to n                        -- long-running task
    call SysSleep 0.01
  end
  return

-- returns running value to be displayed as progress
::method "tenSeconds-progress" unguarded
  expose m n
  return m"/"n "steps done"


-- runs task while displaying progress text at each interval
::class Progress inherit AlarmNotification

-- set progress interval
::method init unguarded
  expose interval
  use strict arg interval = 1

-- starts ticker, runs task, and returns task result
-- (requires names of task and progress methods)
::method monitor
  expose interval object progress
  use strict arg object, task, progress = (task"-progress")
  tick = .Ticker~new(interval, self)
  object~send(task)
  tick~cancel

-- displays progress text at each ticker interval
::method triggered unguarded
  expose object progress
  .stdout~charOut(object~send(progress) '0d'x)