Jump to content
Sign in to follow this  

Combining two infinite loops

Recommended Posts



so i'm trying to create a front end script which will stay idle most of the time.

I've included an option to toggle certain hotkeys in my script using the tray:

while 1
        $msg = TrayGetMsg()
        Case $msg = $tray[0]
            $toggle[0] = Not $toggle[0]
            if $toggle[0] Then
                $tHot = "tHot"
                $tHot = "tOff"
        Case $msg = $tray[1]
            $toggle[1] = Not $toggle[1]
            if $toggle[1] Then
                $yHot = "yHot"
                $yHot = "yOff"
        Case $msg = $tray[2]
            $toggle[2] = Not $toggle[2]
            if $toggle[2] Then
                $hHot = "hHot"
                $hHot = "hOff"

I also have another infinite loop that I want to process at the same time; this one calls PixelGetColor and WinActive among other functions. so the Tray loop is running as fast as possible, while the second loop is running with 3 seconds between each iteration. Since these two have different time frames (the first one "infinitely fast", the second 3000ms) i'm having trouble combining the two.

I've tried using a timer for the second loop, but it doesn't seem to be working for me:

$hGUI = WinGetHandle(AutoItWinGetTitle())
$timer = _Timer_SetTimer($hGUI, 5000, "check")
func check()

I want this script to be as lightweight as possible during its idle time.

So my question is how do i combine two infinite loops with different timings without losing too much efficiency? i was thinking of using TimerInit and TimerDiff, but is running TimerDiff at max looping speed going to affect performance?

And if I have the choice between a Timer and one loop, which one would be more efficient? any help would be appreciated.

Share this post

Link to post
Share on other sites

There have been other solutions posted, but here is at least one using a simple 2D array.

_Func0() is repeated as quickly as possible.

_Func1() run every time the AdLib runs because the timer is set to zero.

_Func2() runs every three seconds.

_Func3() runs every one minute.

The AdLibEnable() function is set to check the other functions twice per second (every 500ms), keeping those functions from taking too much CPU time from the rest of the script.

; Array of timers:
;   [0][0] = count
;   [n][0] = func to call
;   [n][1] = time to next run (in millisec)
;   [n][2] = timer value
$avTimers[4][3] = [[3, ""], _
        ["_Func1", 0, TimerInit()], _
        ["_Func2", 3000, TimerInit()], _
        ["_Func3", 60000, TimerInit()]]     

AdlibEnable("_CheckTimers", 500)

While 1
    _Func0(); Repeat a quickly as possible

Func _CheckTimers()
    For $n = 1 To $avTimers[0][0]
        If TimerDiff($avTimers[$n][2]) < $avTimers[$n][1] Then ContinueLoop
        $avTimers[$n][2] = TimerInit()


Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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