Jump to content

Recommended Posts

Posted

..didn't want to If @HOUR & ":" & @MIN = "03:00" Then  constantly kind of thing. This is simpler.

#include <Date.au3>

; Target execution time (24-hour format: "HH:MM:SS")
Global $g_sTargetTime = StringTrimLeft(_DateAdd('s', 1, _NowCalc()), 11)  ; in 1 sec for this test ; "03:00:00"

; Start initial schedule
Adlib_ScheduleNextRunAt(MyDailyTask, $g_sTargetTime)

While 1
;~  If @HOUR & ":" & @MIN = "03:00" Then MyDailyTask() ; replaced by Adlib_ScheduleNextRunAt()
    Sleep(1000)
WEnd

Func MyDailyTask()
    AdlibUnRegister(MyDailyTask)

    ; --- DO YOUR WORK HERE ---
    ConsoleWrite("- Task executed at: " & _NowCalc() & "." & @MSEC & @CRLF)
    ; --------------------------

    ; Recalculate and re-arm the timer for tomorrow's target time
    Adlib_ScheduleNextRunAt(MyDailyTask, $g_sTargetTime)
EndFunc   ;==>MyDailyTask

Func Adlib_ScheduleNextRunAt($Func, $sTime)
    ConsoleWrite('+ Func Adlib_ScheduleNextRunAt(' & (IsFunc($Func) ? FuncName($Func) : String($Func)) & ", " & $sTime & ')' & @CRLF)
    Local $iMS = Adlib_GetMsUntilNextTime($sTime)
    AdlibRegister($Func, $iMS)
    ConsoleWrite("Next run scheduled in " & Round($iMS / 1000 / 60, 1) & " minutes." & @CRLF)
EndFunc   ;==>Adlib_ScheduleNextRunAt

Func Adlib_GetMsUntilNextTime($sTime)
    Local $sTargetDate, $sNow = _NowCalc()
    Local $sTodayTarget = @YEAR & "/" & @MON & "/" & @MDAY & " " & $sTime

    ; If target time today is still in the future, target today; otherwise target tomorrow
    If _DateDiff('s', $sNow, $sTodayTarget) > 0 Then
        $sTargetDate = $sTodayTarget
    Else
        $sTargetDate = _DateAdd('d', 1, $sTodayTarget)
    EndIf

    Local $iDiffMS = _DateDiff('s', $sNow, $sTargetDate) * 1000
    ConsoleWrite('+ GetMsUntilNextTime(' & $sTime & ') : ' & $iDiffMS & ' ms.' & @CRLF)
    Return $iDiffMS
EndFunc   ;==>Adlib_GetMsUntilNextTime

Maybe more of an idea than an example. But a good idea I think :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting  image.gif.922e3a93535f431de08b31ee669cc446.gif
autoit_scripter_blue_userbar.png

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
×
×
  • Create New...