Jump to content

Timers Crashing


Miliardsto
 Share

Recommended Posts

I do my timers like this by clicking checkbox function is called and timer is set up.

There is a trouble cause I got so many Timers, 20 like this below and I can see if they is launched 5 timers program starts freezing and often crashing.

I know it takes memory but is is right way to do timers like that or what should i do?

Case $checkerSS
                If GUICtrlRead($CheckerOne) = 1 Then
                    $ss = _Timer_SetTimer($Form1,4000,"checkScreen")
                Else
                    _Timer_KillTimer ($Form1,$ss)
                EndIf

            Case $checkerPos
                If GUICtrlRead($DiscAlert) = 1 Then
                    $disc = _Timer_SetTimer($Form1,20000,"checkPosition")
                Else
                    _Timer_KillTimer ($Form1,$disc)
                EndIf

            Case $checkerVars
                If GUICtrlRead($AlertPlayerPos) = 1 Then
                    $ap = _Timer_SetTimer($Form1,15000,"checkVars")
                Else
                    _Timer_KillTimer ($Form1,$ap)
                EndIf

 

Link to comment
Share on other sites

_Timer_SetTimer is gonna register a lot of callback functions, which could be fine but it seems to be breaking your script. Perhaps some of your functions are overlapping because they're taking too long to finish? If you use TimerInit it's just gonna return an int value that can be used in TimerDiff to get the passed time, no callbacks needed. Instead of registering 20 callbacks you could create 20 variables from TimerInit and keep track of the timers yourself

HotKeySet("{Esc}", Close)

Global $aTimerArray[20][3]

For $i = 0 to 19
    $aTimerArray[$i][0] = TimerInit()           ; Initialize a timer
    $aTimerArray[$i][1] = MyFunc                ; Function to call when timeout hit
    $aTimerArray[$i][2] = Random(1000, 1500, 1) ; Timeout when to call the function in the [$i][1]
Next

While (True)
    For $i = 0 to 19
        ; If the difference between the timer in [$i][0] is greater than the timeout set, call the function stored in [$i][1]
        ; Passing the timer by reference so the MyFunc function resets it automatically, you could also reset it in this if statement
        If (TimerDiff($aTimerArray[$i][0]) > $aTimerArray[$i][2]) Then $aTimerArray[$i][1]($aTimerArray[$i][0])
    Next
    Sleep(100)
WEnd

Func MyFunc(ByRef $iTimer)
    ConsoleWrite("MyFunc called after " & TimerDiff($iTimer) & "ms for this timer" & @LF)
    $iTimer = TimerInit()
EndFunc

Func Close()
    Exit 0
EndFunc

When it finishes calling the function in [$i][1] even if the next timer is WAYYYY over the timeout, it will still call the function and reset the timer and move on.

Link to comment
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
 Share

×
×
  • Create New...