Jump to content

schedule execution of date and time in autoit


Go to solution Solved by Melba23,

Recommended Posts

Posted

Good morning colleagues from Autoit,
It is possible to program in autoit, a script that runs every day at the same time.
example every Friday at 9 a.m.

without using windows task scheduler.

Thank you

  • Moderators
Posted

stiv,

Of course. You can use the @Date and @Time macros to specify the point in time you require. Then use a series of If statements in  a loop to check when you are getting close - and speeding up the loop as you do so. There are other ways too, but that is a good way to start as it gives yo lots of practice in coding.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

two minutes before 9:00, checking user inactive

#include <Date.au3>
#include <Timers.au3>

Global $Year, $Mon, $Day
$Year = @YEAR
$Mon = @MON
$Day = @MDAY

While 1 ;use infinite loop since ExitLoop will get called
    If _DateDiff('n', _NowCalc(), $Year & "/" & $Mon & "/" & $Day & " 09:00:00") < 2 Then
        ;https://stackoverflow.com/questions/3867584/autoit-how-to-get-system-idle-time-or-if-screensaver-is-active
        If _Timer_GetIdleTime() > 30000 Then
            ExitLoop
        EndIf
    EndIf
    Sleep(60001) ; 1 minute
WEnd
;Similar
;Do
    ;Sleep(60001)
;Until _DateDiff('n', _NowCalc(), $Year & "/" & $Mon & "/" & $Day & " 09:00:00") < 2


ConsoleWrite("something" & @CRLF)

 

Edited by robertocm
  • Moderators
  • Solution
Posted

Hi,

The Devs were discussing this a while ago and came up with two possible workarounds for the standard AutoIt include functions to prevent this scenario. Here are the options for _NowCalc:

Func _NowCalc_Atomic()
    Local $tStamp = _Date_Time_GetLocalTime()
    Return DllStructGetData($tStamp, "Year") & "/" & _
            StringFormat("%02s", DllStructGetData($tStamp, "Month")) & "/" & _
            StringFormat("%02s", DllStructGetData($tStamp, "Day")) & " " & _
            StringFormat("%02s", DllStructGetData($tStamp, "Hour")) & ":" & _
            StringFormat("%02s", DllStructGetData($tStamp, "Minute")) & ":" & _
            StringFormat("%02s", DllStructGetData($tStamp, "Second"))
EndFunc   ;==>_NowCalc_Atomic

Func _NowCalc_Check()
    Local $sRet, $sCheck
    Do
        $sCheck = @SEC
        $sRet = @YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC
    Until @SEC = $sCheck
    Return $sRet
EndFunc   ;==>_NowCalc_Check

The first of these options will be incorporated for all such functions in the next AutoIt release - and please, please do not ask me when that will be!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)
  On 9/30/2021 at 5:16 PM, Zedna said:

But YES you are right, my script is very simple but not as acurate.

Expand  

Just to be clear, I think your script is accurate, except for once in a blue moon it fires ~1 hour after the last time. The Sleep(60001) does not prevent it since it happens 59 minutes after the sleep is expired.

How often does it glitch?  I wrote this to test:

#include <Date.au3>

Local $sNow, $sThen, $iCalls=0, $htimer=TimerInit()

Do
   $sThen=$sNow
   $sNow=_NowCalc()
   $iCalls+=1
Until $sNow<$sThen

ConsoleWrite("Time jumped from "& $sNow &" to "& $sThen &@CRLF)
ConsoleWrite("Run Time: "& Round(TimerDiff($htimer)/1000,3) &@CRLF)
ConsoleWrite("Calls Made: "& $iCalls  &@CRLF)

This uses the current _NowCalc() function which returns a concatenation of date macros, and suffers from the same rollover glitch, except at the @SEC level. But it’s rare - below it happened 1 time in 13 million tests.  So your script might have gone 10 million days without an error.

Time jumped from 2021/05/20 21:01:00 to 2021/05/20 21:01:59
Run Time: 50.323
Calls Made: 13602962

 

Edited by JockoDundee

Code hard, but don’t hard code...

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...