El-Masry Posted April 22, 2019 Share Posted April 22, 2019 (edited) Hello every one, I wrote this function which can be used to do a task in the main loop without using : Sleep() inspired : https://www.autoitscript.com/forum/topic/198580-check-every-10sec-_imagesearch/ .... Here is the function : expandcollapse popup;Timer_Task.au3 #include <Array.au3> #Region Example Local $counter = 1 While True _Loop_Task("_Test()", 1, 1000) ConsoleWrite("Counter: " & $counter & @CRLF) $counter += 1 Sleep(100) Wend Func _Test() MsgBox(0, 0, 0) EndFunc #EndRegion Example ; #FUNCTION# ===================================================================================================================== ; Name ..........: _Loop_Task ; Description ...: Doing a Task in the main loop without using Sleep() ; Syntax ........: _Loop_Task($Task, $Index, $MSEC_To_Wait) ; Parameters ....: $Task - A string of the task ..like this : "MsgBox(0, 0, 0)" ; $Index - A unique value which specifies the task , it is unique as 2 tasks can't have the same index ; $MSEC_To_Wait - the number of Milliseconds to wait...the task will be done every ($MSEC_To_Wait) Milliseconds ; Return values .: None ; Author ........: El-Masry : إبراهيم عصام الدين يوسف ; Email .........: iimosa7777@gmail.com ; Written in ....: ISN Autoit Studio ; With Example ..: Yes ; Inspired.......: https://www.autoitscript.com/forum/topic/198580-check-every-10sec-_imagesearch/ ; ================================================================================================================================ Func _Loop_Task($Task, $Index, $MSEC_To_Wait) ;note : #include <Array.au3> If IsDeclared("Task_Index_3425") == 0 Then Global $Task_Index_3425[] = [0] If _ArraySearch($Task_Index_3425, $Index) == -1 Then _ArrayAdd($Task_Index_3425, $Index) $Task_ID = _ArraySearch($Task_Index_3425, $Index) ;ConsoleWrite("Task ID = " & $Task_ID& @CRLF) If IsDeclared("Timer_Start_3425_" & $Task_ID) Then $M_Time = TimerDiff(Eval("Timer_Start_3425_" & $Task_ID)) ;ConsoleWrite("M_Time = " & $M_Time & @CRLF) If IsDeclared("Task_3425_" & $Task_ID) == 0 Then Assign("Task_3425_" & $Task_ID, 0, 2) ;ConsoleWrite("Waited Time = " & Eval("Task_3425_" & $Task_ID)& @CRLF) Assign("Task_3425_" & $Task_ID, Eval("Task_3425_" & $Task_ID) + $M_Time, 2) ;ConsoleWrite(Eval("Task_3425_" & $Task_ID)& @CRLF) If Eval("Task_3425_" & $Task_ID) >= $MSEC_To_Wait Then Execute($Task) Assign("Task_3425_" & $Task_ID, 0, 2) EndIf EndIf Assign("Timer_Start_3425_" & $Task_ID, TimerInit(), 2) EndFunc Have fun with it Edited April 22, 2019 by El-Masry Adding @seadoggie01 example Zag8888 1 Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted April 22, 2019 Moderators Share Posted April 22, 2019 So out of curiosity, what does this buy someone rather than doing AdlibRegister? El-Masry 1 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
seadoggie01 Posted April 22, 2019 Share Posted April 22, 2019 Also, you might consider cutting down your loop timing... if you Sleep(1000), your code just looks like a fancy message box Consider an example something a bit more like this: #Region Example Local $counter = 1 While True _Loop_Task("_Test()", 1, 1000) ConsoleWrite("Counter: " & $counter & @CRLF) $counter += 1 Sleep(100) Wend Func _Test() MsgBox(0, 0, 0) EndFunc #EndRegion Example Which shows your code being evaluated, but not executing the _Test function each loop El-Masry 1 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
El-Masry Posted April 22, 2019 Author Share Posted April 22, 2019 1 hour ago, JLogan3o13 said: So out of curiosity, what does this buy someone rather than doing AdlibRegister? You are right.. AdlibRegister .. is more simble I didn't know about it Link to comment Share on other sites More sharing options...
El-Masry Posted April 22, 2019 Author Share Posted April 22, 2019 (edited) 1 hour ago, seadoggie01 said: Also, you might consider cutting down your loop timing... if you Sleep(1000), your code just looks like a fancy message box Consider an example something a bit more like this: #Region Example Local $counter = 1 While True _Loop_Task("_Test()", 1, 1000) ConsoleWrite("Counter: " & $counter & @CRLF) $counter += 1 Sleep(100) Wend Func _Test() MsgBox(0, 0, 0) EndFunc #EndRegion Example Which shows your code being evaluated, but not executing the _Test function each loop OK.. I will update it 👍 Edited April 22, 2019 by El-Masry Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted April 22, 2019 Moderators Share Posted April 22, 2019 It is good to want to improve upon the language; generally a good idea to become familiar with it first. El-Masry 1 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! 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