Dizzy Posted July 20, 2013 Share Posted July 20, 2013 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 More sharing options...
water Posted July 20, 2013 Share Posted July 20, 2013 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 2024-07-28 - Version 1.6.3.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 (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 More sharing options...
czardas Posted July 20, 2013 Share Posted July 20, 2013 (edited) 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. Edited July 20, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
0xdefea7 Posted July 21, 2013 Share Posted July 21, 2013 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 More sharing options...
kylomas Posted July 21, 2013 Share Posted July 21, 2013 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 0xdefea7 1 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 More sharing options...
mikell Posted July 21, 2013 Share Posted July 21, 2013 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 More sharing options...
czardas Posted July 21, 2013 Share Posted July 21, 2013 (edited) 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 July 21, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Dizzy Posted July 22, 2013 Author Share Posted July 22, 2013 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 More sharing options...
Solution BrewManNH Posted July 22, 2013 Solution Share Posted July 22, 2013 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 GudeHow 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 More sharing options...
Dizzy Posted July 22, 2013 Author Share Posted July 22, 2013 libRegister seems to be the right command for me ... I'll have to check this over some hours. Dizzy Link to comment Share on other sites More sharing options...
czardas Posted July 22, 2013 Share Posted July 22, 2013 (edited) 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 July 22, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Dizzy Posted July 22, 2013 Author Share Posted July 22, 2013 @BrewManNH : Yes, you're right ... sometimes life is so easy. Thanks for your solution. Dizzy Link to comment Share on other sites More sharing options...
BrewManNH Posted July 22, 2013 Share Posted July 22, 2013 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 GudeHow 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 More sharing options...
czardas Posted July 22, 2013 Share Posted July 22, 2013 (edited) The drift does not have to be any more than that of the internal PC clock - totally negligible. This has nothing to do with AutoIt. Edited July 22, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
kylomas Posted July 22, 2013 Share Posted July 22, 2013 (edited) 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 July 22, 2013 by kylomas czardas 1 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 More sharing options...
czardas Posted July 22, 2013 Share Posted July 22, 2013 (edited) 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 July 22, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
BrewManNH Posted July 22, 2013 Share Posted July 22, 2013 @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 GudeHow 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 More sharing options...
kylomas Posted July 22, 2013 Share Posted July 22, 2013 (edited) @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 July 22, 2013 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 More sharing options...
czardas Posted July 22, 2013 Share Posted July 22, 2013 (edited) @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 July 22, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
czardas Posted July 22, 2013 Share Posted July 22, 2013 (edited) 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 July 22, 2013 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now