Proc's For Waiting (after)

Created by CecilWesterhof.

I wrote some proc's to wait until a certain moment, or get the ticks to wait to a certain moment. They can be used to wait in a script, or used with the event loop.

First the proc to wait to a certain minute.

Often you want to do something every minute, or every five minutes, or every 15 minutes, or ...

For this I wrote the following proc:

# Waits until minutes is a multiply of INTERVAL
# 60 % INTERVAL should be 0
proc waitMinutes {interval} {
    if {[expr {(60 % $interval) != 0}]} {
        error "[getProcName]: 60 is not a multiply of $interval"
    }
    after [getWaitMinutesTicks $interval]
}

This function waits until the start of the next multiply of interval minutes. For example when using an interval of fifth-teen it waits until the start of the 0, 15, 30 or 45 minute.

I have a similar function that waits for seconds:

# Waits until seconds is a multiply of INTERVAL
# 3600 % INTERVAL should be 0
proc waitSeconds {interval} {
    if {[expr {(3600 % $interval) != 0}]} {
        error "[getProcName]: 3600 is not a multiply of $interval"
    }
    after [getWaitSecondsTicks $interval]
}

For these two proc's I need two other procs:

# Get ticks to wait until minutes is a multiply of INTERVAL
# 60 % INTERVAL should be 0
proc getWaitMinutesTicks {interval} {
    if {[expr {(60 % $interval) != 0}]} {
        error "[getProcName]: 60 is not a multiply of $interval"
    }
    getWaitSecondsTicks [expr {$interval * 60}]
}

# Get ticks to wait until seconds is a multiply of INTERVAL
# 3600 % INTERVAL should be 0
proc getWaitSecondsTicks {interval} {
    if {[expr {(3600 % $interval) != 0}]} {
        error "[getProcName]: 3600 is not a multiply of $interval"
    }
    set secondsInHour [expr {[clock seconds] % 3600}]
    set secondsToWait [expr {$interval - ($secondsInHour % $interval)}]
    expr {1000 * $secondsToWait}
}

When I want to wait for the start of the next minute, I use:

waitMinutes 1

When I want to use it with an event loop, I do something like:

proc storeCPUTemp {} {
    storeMessage cpu-temp [getCPUTemp]
    after [getWaitMinutesTicks $::intervalCPU]  storeCPUTemp
}

When the proc's are called with the wrong parameter, they generate an error and use getProcName: GetProcName/GetScriptName.

If you find them useful and are going to use them: let me know.

As always: comments, tips and questions are appreciated.