Sign in to follow this  
Followers 0
cppman

Simple Timers

5 posts in this topic

#1 ·  Posted (edited)

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
        _TimerCall($t_TIMER_OBJECT)
        $t_TIMER_OBJECT[4] += 1
        $t_TIMER_OBJECT[2] = $t_TIMER_OBJECT[1]
        $t_TIMER_OBJECT[3] = TimerInit()
        return ($t_TIMER_OBJECT)
    Else
        return ($t_TIMER_OBJECT)
    EndIf
EndFunc

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

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
        Call($Timer[0])
        ;we need to reset the information now
        $Timer[2] = $Timer[1]
        $Timer[3] = TimerInit()
        $Timer[4] += 1
        return true
    Else
        $Timer[2] = (($Timer[1]) - (TimerDiff($Timer[3])))
        return false
    EndIf
EndFunc

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

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

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

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

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

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

Func _TimerCall($Timer)
    ;Calls the timer's callback
    call($Timer[0])
    return True
EndFunc
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)
while(true)
    ;update the Timer
    _TimerUpdate($MyTimer)
    _TimerUpdate($AnotherTimer)
WEnd

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

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

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



I could have really used this when I made this.


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

I thought you left the AutoIt community, chris?

Share this post


Link to post
Share on other sites

I thought you left the AutoIt community, chris?

I did.. for a while. I saw the AutoIt icon sitting on my desktop and couldn't resist :).

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  
Followers 0