SyncTimer - 1.2

timers synchronized with clock

Table Of Contents

NAME

SyncTimer - timers synchronized with clock.

SYNOPSIS

package require SyncTimer

::SyncTimer::every interval basetime script ?script ...?

::SyncTimer::cancel id

INTRODUCTION

This package provides commands able to start/stop synchronized timers. Each time a timer is triggered, it executes a user-defined command. Timers are triggered at regular intervals, and are aligned (synchronized) with the system-clock.

::SyncTimer::every interval basetime script ?script ...?
This command creates and starts a new timer in sync with the system-clock.

interval is a specification of the interval; it should be expressed as a "relative time" (see [clock] command).

basetime is the time-displacement

Usually it is "00:00:00", meaning that timer should be aligned with system-clock. It can be any "relative time specification" (see [clock] command).

script ?script ...?

The triggered command is formed by concatenating all the script arguments in the same fashion as the concat command (see syntax for [after] command).

The SyncTimer::every command returns an identifier that can be used later to cancel the timer.

::SyncTimer::cancel id
This command stops the timer whose handler is id (id must be the return value from a previous SyncTimer::every command).

CLOCK PRECISION and TUNING

EXAMPLE

Just a simple echo of current time, followed by a msg

   proc myCmd {msg} {
     puts -nonewline "[clock format [clock seconds] -format "%Y-%m-%d %T"]"
     puts " $msg"
   }
          
   set t1  [SyncTimer::every "10 secs" "00:00:00" {myCmd "Timer-One - every 10 secs"}]
   set t2  [SyncTimer::every "15 secs" "00:00:00" {myCmd "Timer-Two - every 15 secs"}]
   set t1a [SyncTimer::every "15 secs" "00:00:02" {myCmd "every 15 secs (+2)"}]
      
    # ... let them run for some minutes ...
      
   SyncTimer::cancel $t2
   SyncTimer::cancel $t1a
   SyncTimer::cancel $t1

NOTES

Event Loop
The SyncTimer command assumes that the application is event driven: the scheduled commands will not be executed unless the application enters the event loop. In applications that are not normally event-driven, such as tclsh, the event loop can be entered with the vwait and update commands. ( see also [after] command )
Min Interval
1 second
Max Interval
2**31 msec ( about 24 days and 20 hours)
No Delay Drift
Unexpected delays (due to the underlying event loop) are NOT summed up. If for any reason, a scheduled timer is started with some delay dT, the next time timer is triggered, it wont't sum up this delay.
Precision
Using SyncTimer, timers are synchronized with system clock with a precision of 1 millisecond. (Apart of delay due to the CPU scheduling).

HISTORY