Jump to content
Sign in to follow this  
FaridAgl

Simple Timers UDF

Recommended Posts

FaridAgl

Here is my wrapper for SetTimer and KillTimer APIs.
AutoIt already has its own, but I tried to make it as simple as I could.

#include-once

Global $g_avSimpleTimers = [[0, 0]]

Func TimerSet($vFunction, $uInterval)
    Local $hFunction = DllCallbackRegister($vFunction, "none", "HWND;UINT;UINT_PTR;DWORD")
    If ($hFunction = 0) Then Return 0

    Local $uTimerId = DllCall("user32.dll", "UINT_PTR", "SetTimer", _
            "HWND", 0, _
            "UINT_PTR", 0, _
            "UINT", $uInterval, _
            "ptr", DllCallbackGetPtr($hFunction))[0]

    If ($uTimerId = 0) Then
        DllCallbackFree($hFunction)
        Return 0
    EndIf

    ReDim $g_avSimpleTimers[$g_avSimpleTimers[0][0] + 2][2]
    $g_avSimpleTimers[$g_avSimpleTimers[0][0] + 1][0] = $uTimerId
    $g_avSimpleTimers[$g_avSimpleTimers[0][0] + 1][1] = $hFunction
    $g_avSimpleTimers[0][0] += 1

    Return $uTimerId
EndFunc

Func TimerUnset($uTimerId)
    If ($g_avSimpleTimers[0][0] = 0) Then Return False

    Local $iCount = $g_avSimpleTimers[0][0]
    For $i = 1 To $iCount + 1
        If ($g_avSimpleTimers[$i][0] = $uTimerId) Then
            DllCall("user32.dll", "BOOL", "KillTimer", _
                    "HWND", 0, _
                    "UINT_PTR", $uTimerId)

            DllCallbackFree($g_avSimpleTimers[$i][1])

            For $j = $i To $iCount - 1
                $g_avSimpleTimers[$j][0] = $g_avSimpleTimers[$j + 1][0]
                $g_avSimpleTimers[$j][1] = $g_avSimpleTimers[$j + 1][1]
            Next

            $g_avSimpleTimers[0][0] = $iCount - 1
            ReDim $g_avSimpleTimers[$iCount][2]

            Return True
        EndIf
    Next

    Return False
EndFunc

Example:

#include "Simple Timers.au3"

Global $t1 = TimerSet(Foo, 500)
Global $t2 = TimerSet("Bar", 1000)

MsgBox(0, "", "Check the console.")

TimerUnset($t1)

MsgBox(0, "", "Check the console again.")

TimerUnset($t2) ;Not necessary if you are exiting the script

Func Foo($a, $b, $c, $d)
    ConsoleWrite(">Foo" & @CRLF)
EndFunc

Func Bar($a, $b, $c, $d)
    ConsoleWrite("!Bar" & @CRLF)
EndFunc

Download both UDF and example:

Edited by D4RKON3

Share this post


Link to post
Share on other sites
FaridAgl

Thread updated:

- Some changes on how the UDF was using its internal global array.

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  

  • Similar Content

    • mrtgtr
      By mrtgtr
      I want to do something , after wait 2 min and again do same thing
      But I do do not want with sleep, it must be timer 
      How I do this ?
    • Miliardsto
      By Miliardsto
      Is there any difference in timers and AdlibRegister ?
      in :
      performance usage reliability or any kind of difference I want use few timers at once so it will make difference.
      I have problem with timers and I saw AdlibRegister behaves similar like:
      wont stop crashing slowing program so much maybe is there any trick to make it better?
    • Miliardsto
      By Miliardsto
      Hello I got errors and timers not work if I use any obfuscator(stripper). All code and things work great after obfuscate but functions with timers dont.
      -### StripOnly/StripFunc Error: Found DllCallbackRegister() statement using unsolvable Func, which will/could lead to removal of Funcs that are used by this Function.  
      Here are scripts U can try use stripper then functions would not work
      https://www.autoitscript.com/autoit3/docs/libfunctions/_Timer_SetTimer.htm
       
      Here is written I believe something on this topic https://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/Au3Stripper.html
       
       
       
       
      Could I do something with that? I got several functions with timers and I dont want use ignore these functions
      #Au3Stripper_Ignore_Funcs=  
    • TotalAuto
      By TotalAuto
      Howdy All
      I've gone around and around in circles wasting hours trying to fix this problem to no avail so I thought I would try posting for help given there are some real smart people in this forum.
      Background
      I have 5 separate .au3 scripts that all do different basic tasks. For example:
      1.au3 = opens Chrome browser to obtain information from a specific website then closes
      2.au3 = opens an Excel workbook to run a macro then saves and closes
      3.au3 = Open two seperate workbooks and transfers data from one to the other then saves and closes
      4.au3 = .......etc
      5.au3 = .......etc
      These 5 separate scripts, doing completely different tasks, need to each run once per day at 5 different designated times within a day and then close. For example:
      1.au3 = 10am
      2.au3 = 12pm
      3.au3 = 2pm
      4.au3 = 4pm
      5.au3 = 5pm
      Problem
      I can not get these different scripts to reliably run at their designated times. When I manually run these scripts individually by using 'Go F5' they work perfectly. If I convert these scripts to an .exe and manually run each one they work about 98% of time.
      How can I schedule these different .au3 files or associated .exe files to reliably run each day at different times?
      Attempts To Fix The Issue
      1. I have tried converting each .au3 script into their own .exe file and use Window's Task Scheduler to run these .exe files at their designated time but the success rate  of running each .exe in full, to completion, without error is very low at about 30%. Things that commonly go wrong include a browser not opening but movement still continues on the desk top. Other times things just do not happen at all.
      2. I have Windows 7 64 bit and have tried creating and running both (x86) and (x64) .exe files but again reliability problems exist;
      3. I have also tried moving each .exe file to the My Documents folder as I read someone that keeping your files on your desktop ties your files to individual Windows user profiles which can cause AutoIt operating errors. Not sure if this is correct but thought I would try it anyway. It did not fix my issue.
      4. I installed a program called AlwaysUp which supposedly always reliably runs in the background and will reliably execute your AutoIt script or .exe files. Still have significant reliability issues.
      Therefore, can someone please provide advice on how I can get my 5 different .au3 files to run at 5 designated times through the day, every day, without fail? Overtime I will be creating more and more different .au3 files to run certain tasks throughout the day and do not want to create one large AutoIt file that continually runs and attempts to do all the different tasks itself.
      Any help is very appreciated.
      Thanks
      Dan
       
       
    • jannikrendl
      By jannikrendl
      Hey guys,
      I would like to create a simple Timer which starts counting down if a key is pressed and after running out of time making a sound. Afterwards it should reset and wait again for the same key to be pressed to start again.
       
      I am new to autoit and really don't know how to get started...
      How I would try to do it:
      while(1) WEnd HotKeySet("{F9}", "_Timer") Func _Timer() ;FUNCTION HERE but I don't know what to do there :& EndFunc Best regards and thanks in advance
       
      JR
×