Jump to content
Sign in to follow this  

Simple Timers

Recommended Posts

I haven't worked with AutoIt for a while now, but I thought I'd mess around with it again. I originally wrote this in C++ (view it here) and decided to try it in AutoIt.

This is a simple timer library where you can create multiple timers. The timer will call the function you specify every n milliseconds.

Here is the include file: Timer.au3

Global Const $TL_STARTIMM = 0
Global Const $TL_STARTAFT = 1

Func _TimerCreate($Callback, $Interval = 1000, $Start = 1)
    ;Adds the callback to the queue
    Local $t_TIMER_OBJECT[5]
    $t_TIMER_OBJECT[0] = $Callback
    $t_TIMER_OBJECT[1] = $Interval
    $t_TIMER_OBJECT[2] = $Interval
    $t_TIMER_OBJECT[3] = TimerInit()
    $t_TIMER_OBJECT[4] = 0 ; call count
    if ($Start == $TL_STARTIMM) Then
        $t_TIMER_OBJECT[4] += 1
        $t_TIMER_OBJECT[2] = $t_TIMER_OBJECT[1]
        $t_TIMER_OBJECT[3] = TimerInit()
        return ($t_TIMER_OBJECT)
        return ($t_TIMER_OBJECT)

Func _TimerReset(ByRef $Timer)
    $Timer[1] = 1000
    $Timer[2] = 1000
    $Timer[3] = TimerInit()
    $Timer[4] = 0
    return True

Func _TimerUpdate(ByRef $Timer) ;Returns true if the function was called
    ;Checks if it is time to call the callback
    ;First it needs to check if it is time to Call
    if ($Timer[2] <= 0) Then
        ;we need to reset the information now
        $Timer[2] = $Timer[1]
        $Timer[3] = TimerInit()
        $Timer[4] += 1
        return true
        $Timer[2] = (($Timer[1]) - (TimerDiff($Timer[3])))
        return false

Func _TimerGetCallback($Timer)
    ;Returns the callback function
    return ($Timer[0])

Func _TimerGetInterval($Timer)
    ;Returns the interval in milliseconds the timer is set To
    return ($Timer[1])

Func _TimerGetRemainingMs($Timer)
    ;returns the time remaining before the next Call
    ;This function must be called AFTER TimerUpdate()
    return ( Round($Timer[2]*-1) )

Func _TimerSetInterval(ByRef $Timer, $Interval = 1000)
    ;Sets the interval of the specified timer.
    $Timer[1] = $Interval
    return True

Func _TimerSetCallback(ByRef $Timer, $Callback)
    ;Sets the callback of the Timer
    $Timer[0] = $Callback
    return true

Func _TimerGetCallCount($Timer)
    return ($Timer[4])

Func _TimerCall($Timer)
    ;Calls the timer's callback
    return True
oÝ÷ Øw«z+ìZ^¡øhÂÚ.±è­jëh×6
#include "Timer.au3"
$hwnd = GUICreate("Timer Example", 640, 480)
$MyTimer = _TimerCreate("Func_MyTimer", 2000, $TL_STARTIMM)
$AnotherTimer = _TimerCreate("Func_AnotherTimer", 1000, $TL_STARTAFT)
    ;update the Timer

Func Func_MyTimer()
    MsgBox(0, "Timer Example", "Timer Example Using $TL_STARTIMM parameter")

Func Func_AnotherTimer()
    MsgBox(0, "Timer Example", "Timer Example Using $TL_STARTAFT parameter")

and here are all the function prototypes and there descriptions:

_TimerCreate($Callback, $Interval = 1000, $Start = 1) - Creates a timer based on the parameters passed to it.

_TimerReset(ByRef $Timer) - Resets all parameter values to their default.

_TimerUpdate(ByRef $Timer) - Updates all the values. This MUST be called every main loop iteration.

_TimerGetCallback($Timer) - Returns the function name it calls.

_TimerGetInterval($Timer) - Returns the number of milliseconds between each call.

_TimerGetRemainingMs($Timer) - Returns the remaining time before the next call.

_TimerSetInterval(ByRef $Timer, $Interval = 1000) - Changes the amount of time between calls.

_TimerSetCallback(ByRef $Timer, $Callback) - Changes the function it calls.

_TimerGetCallCount($Timer) - Returns the number of times the function has been called.

_TimerCall($Timer) - Overrides the timer and calls the function.


Hope you find this useful..

Edited by chris95219

Share this post

Link to post
Share on other sites

Very nice timer functions.

Do you think one or more of these functions would run under the system account taking arguments from another compiled script running under an admin account?

When no one is logged on the server, other compiled scripts called from the main program do not run. Timer functions are important for a number of needs without pausing the main program execution.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...