Jump to content

loop excatly every 10 seconds


Go to solution Solved by BrewManNH,

Recommended Posts

Hi together,

i want to go through a Loop every 10 seconds. But depending the code inside the loop, the time differs.

E. g.

 Do
   Select
   Case @SEC = 0 or @SEC = 10 or @SEC = 20 or @SEC = 30 or @SEC = 40 or @SEC = 50
      ... do something ...

   end select

 Until $bHotKeyPressed

 How can i be sure that the loop will be done exactly on the 10'th of a second?

Thanks for your help!

Dizzy

Link to comment
Share on other sites

Some questions come to my mind:

How inaccurate is your code right now?

What do you need this for? Sounds a bit strange.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Use a Timer:

TimerInit()
TimerDiff()

And keep this in mind:

 

The answer is that you can only do this if the 'do something' takes less than 10 seconds - approx 9.99 secs maximum (maybe less) in order to remain within a 10 second loop.
Link to comment
Share on other sites

Dizzy,

If you want to use the @SEC macro you can do something like this

#include <date.au3>

while 1

    if mod(@sec,10) = 0 then ConsoleWrite(_now() & @LF)
    sleep(1000)

wend

However, before we can go any further we need to know what application this is for.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

It should be possible to get around the 10s max issue for the 'do something' task by the use of AdlibRegister, but if a new 'do something' starts while the previous one is still running this can lead to strange results ^^

Link to comment
Share on other sites

I don't think the OP knows exactly what he or she wants. There are several possibilities but it's pure guesswork. There are many ways to trigger events with the possibility of duplicated calls, interrupted processes, time catch up and time out scenarios. 'do something' every 10 seconds is just too vague to give a clear answer.

Edited by czardas
Link to comment
Share on other sites

Hi,

thanks for reply.

If tried the timerinit/ -diff function but this won't fit my task.

E.g.

 

Local $begin = TimerInit()
$a = 1
 
Do
Sleep(3000)
Local $dif = TimerDiff($begin)
ConsoleWrite("Info:" & $dif & @crlf)
until $a = 0
 
The result is following:
Info:2998.8729
Info:5998.9561
Info:8999.3238
Info:11999.4497
Info:15000.0982
Info:18000.2154
Info:21000.6013
Info:24001.1772
Info:27001.6252
Info:30002.4631
Info:33001.9012
Info:36002.9748
Info:39003.6952
Info:42002.9822
 
My job should run 24 hours or more ...
 
I will save some system information exactly every 10 seconds (and 'yes' - the job will be done in less than 10 seconds ;-)).
 
Is there a way to do this?
@kylomas: i think this wan't work. If i use 2300 for the sleep, i will miss some 10'th steps.
 
Thanks
Dizzy
Link to comment
Share on other sites

  • Solution

This is so incredibly easy to accomplish.

Global $Timer = TimerInit(), $Diff = 0
While 1
    $Diff = TimerDiff($Timer)
    If $Diff >= 10000 Then
        ConsoleWrite($Diff / 1000 & @CRLF)
        $Timer = TimerInit()
    EndIf
    Sleep(10) ; so you don't burn up your CPU or use too much of it
WEnd

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I imagine using AdlibRegister will produce more or less the same set of results as the code posted by brewmanNH. There may be a small drift factor with both methods - I'm not sure. This will occur with BrewmaNH's method without any modification. Using the @Sec macro combined with TimerDiff and Sleep may be more accurate (approx 10 to 11 ms delay, but with no overall drift). I have no time now, but someone should be able to figure this out if need be.

Edited by czardas
Link to comment
Share on other sites

Lets be totally honest here, there's no way that AutoIt will EVER be able to give you something at an exact time consistently. There's going to be drift no matter what you use just because AutoIt is an interpreted language, and even the best personal computer is going to have some kind of clock drift just on its own. Hence the need for setting the clock using NTP servers on practically every OS.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Dizzy,

Have you actually run any of the code offered above?

@kylomas: i think this wan't work. If i use 2300 for the sleep, i will miss some 10'th steps.

 

Where did "2300" come from?  The sleep in the code that I posted is just to stop the loop from streaming output while on the 10 second interval and to idle the CPU.

The solution BremanNH posted produces the same result.  I think czardas hit it on the head, the definition is vague and the target now seems to be moving.

Regardless of whether you use an Adlib, modulo arithmetic or a timer, you will get better results if your requirements are clearly defined.

kylomas

edit: spelling...shit, cross posted...

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Finally I get a miinute to test this. I was working before.

;

HotKeySet("{ESC}", "_Quit")

While 1
    If Not Mod(@SEC, 10) Then
        ConsoleWrite(@HOUR & @MIN & @SEC & @LF) ; Do something
        Sleep(1010) ; Make sure to sleep for more than one second, else the method breaks.
    EndIf
    Sleep(10)
WEnd

Func _Quit()
    Exit
EndFunc

;

Overall drift factor is zero. Accuracy is +- 5 ms.

Edited by czardas
Link to comment
Share on other sites

@czardas

That will work, as long as the OP doesn't mind if the first run through isn't exactly 10 seconds after starting the script. Because if the first run is started at 11 seconds, the consolewrite happens 9 seconds later, if the first run is started at 9 seconds, the consolewrite will happen 1 second later.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@czardas

That will work, as long as the OP doesn't mind if the first run through isn't exactly 10 seconds after starting the script. Because if the first run is started at 11 seconds, the consolewrite happens 9 seconds later, if the first run is started at 9 seconds, the consolewrite will happen 1 second later.

 

Yes, I forgot to point that out in my OP...sorry!

@czardas

Overall drift factor is zero. Accuracy is +- 5 ms.

 

Unless I misunderstand the OP the granularity required is 1 sec.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

@czardas

That will work, as long as the OP doesn't mind if the first run through isn't exactly 10 seconds after starting the script. Because if the first run is started at 11 seconds, the consolewrite happens 9 seconds later, if the first run is started at 9 seconds, the consolewrite will happen 1 second later.

 

Judging by the first post this would be the case. Some additional maths can be thrown in to make small adjustments to the timing. This is a method without drift.

Edited by czardas
Link to comment
Share on other sites

Unless I misunderstand the OP the granularity required is 1 sec.

 

I didn't notice where the OP says that. Anyway the method would need to be different in that case. eg.

;

HotKeySet("{ESC}", "_Quit")

Local $iCurrSec = @SEC

While 1
    If $iCurrSec <> @SEC Then
        ConsoleWrite(@HOUR & @MIN & @SEC & @LF) ; Do something
        $iCurrSec = Mod($iCurrSec +1, 60)
    EndIf
    Sleep(10)
WEnd

Func _Quit()
    Exit
EndFunc

;

If a timer is used instead, it should be set only once at the start. This should also avoid overall drift but I think the @SEC macro method is good.

Edited by czardas
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

  • Recently Browsing   0 members

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