Jump to content

Recommended Posts

Posted (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 :

;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 by El-Masry
Adding @seadoggie01 example
Posted

Also, you might consider cutting down your loop timing... if you Sleep(1000), your code just looks like a fancy message box :D

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

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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Posted
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

Posted (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 :D

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 by El-Masry

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...