#include-once #include Global $g_RegisterLoop[1][2] , $g_WaitWhile_IsWaiting[1][3] $g_RegisterLoop[0][0] = 0 $g_WaitWhile_IsWaiting[0][0] = 0 ;~ _ArrayDisplay($g_WaitWhile_IsWaiting," (Line: "&@ScriptLineNumber&")") ;~ Exit ; ___________________________________________________________________________ ; #FUNCTION# ==================================================================================================================== ; Name ..........: RegisterLoop ; Description ...: Register function so it will run like a child while loop ; Syntax ........: RegisterLoop($FuncName[, $Sleep = 250]) ; Parameters ....: $FuncName - The function to register. ; $Sleep - [optional] what is the sleep in the loop. Default is 250. ; Return values .: None ; @error ; 1 = function is already registered ; 2 = Failed in register the function ; Author ........: gil900 ; Example .......: yes ; =============================================================================================================================== Func RegisterLoop($FuncName,$Sleep = 250) If Array2DSearch($g_RegisterLoop,$FuncName,0) > 0 Then Return SetError(1) If Not AdlibRegister($FuncName,$Sleep) Then Return SetError(2) $g_RegisterLoop[0][0] += 1 ReDim $g_RegisterLoop[$g_RegisterLoop[0][0]+1][2] $g_RegisterLoop[$g_RegisterLoop[0][0]][0] = $FuncName $g_RegisterLoop[$g_RegisterLoop[0][0]][1] = $Sleep _ArraySort($g_RegisterLoop,0,1,0,1) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: UnRegisterLoop ; Description ...: UnRegister a registered function. use this to shut down the selected loop (registered function) ; Syntax ........: UnRegisterLoop($FuncName) ; Parameters ....: $FuncName - the function to unregister ; Return values .: None ; @error: ; 1 = $FuncName is unregistared. ; Author ........: gil900 ; =============================================================================================================================== Func UnRegisterLoop($FuncName) Local $tmp1 = Array2DSearch($g_RegisterLoop,$FuncName,0) If $tmp1 <= 0 Then Return SetError(1) _ArrayDelete($g_RegisterLoop,$tmp1) $g_RegisterLoop[0][0] -= 1 AdlibUnRegister($FuncName) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: WaitWhile ; Description ...: If you want to use sleep in a registered function (loop) that registered with this UDF ; Then in order to avoid from freezing the other loops, you need to use this function as ; an alternative to sleep. ; for example - Instead of using: Sleep(1000) , you need to use WaitWhile('funcname',1000) ; ; ** important: the 'funcname' must be the name of the function that you called from.. ; ; for example: in this case: ; ; Func SomeName() ; .... ; EndFunc ; ; $FuncName need to be 'SomeName' so you call it this way: WaitWhile('SomeName',1000) ; ; Syntax ........: WaitWhile($FuncName, $WaitTime[, $Sleep = 25]) ; Parameters ....: $FuncName - Read the description. ; $WaitTime - How long to wait. ; $Sleep - [optional] how much to sleep while waiting. Default is 25. ; Return values .: None ; @error: ; NONE ; Author ........: gil900 ; Example .......: No ; =============================================================================================================================== Func WaitWhile($FuncName,$WaitTime,$Sleep = 25) Local $SearchFunc = Array2DSearch($g_WaitWhile_IsWaiting,$FuncName,0) If $SearchFunc <= 0 Then $g_WaitWhile_IsWaiting[0][0] += 1 ReDim $g_WaitWhile_IsWaiting[$g_WaitWhile_IsWaiting[0][0]+1][3] $g_WaitWhile_IsWaiting[$g_WaitWhile_IsWaiting[0][0]][0] = $FuncName $g_WaitWhile_IsWaiting[$g_WaitWhile_IsWaiting[0][0]][1] = $WaitTime $g_WaitWhile_IsWaiting[$g_WaitWhile_IsWaiting[0][0]][2] = TimerInit() EndIf ;_ArrayDisplay($g_WaitWhile_IsWaiting," (Line: "&@ScriptLineNumber&")") Local $WaitTimer = TimerInit(),$aTmp = $g_RegisterLoop, _ $tmp1 = Array2DSearch($aTmp,$FuncName,0) If $tmp1 > 0 Then _ArrayDelete($aTmp,$tmp1) $aTmp[0][0] -= 1 EndIf ReDim $aTmp[$aTmp[0][0]+1][3] For $a = 1 To $aTmp[0][0] $aTmp[$a][2] = TimerInit() Next ;_ArrayDisplay($aTmp," (Line: "&@ScriptLineNumber&")") Do For $a = 1 To $aTmp[0][0] ;~ If TimerDiff($WaitTimer) >= $WaitTime Then ExitLoop 2 If $g_WaitWhile_IsWaiting[0][0] > 0 Then $tmp1 = Array2DSearch($g_WaitWhile_IsWaiting,$aTmp[$a][0],0) If $tmp1 > 0 And TimerDiff($g_WaitWhile_IsWaiting[$tmp1][2]) < $g_WaitWhile_IsWaiting[$tmp1][1] Then ContinueLoop EndIf If _ArraySearch($g_WaitWhile_IsWaiting,$aTmp[$a][0]) >= 0 Then ContinueLoop If TimerDiff($aTmp[$a][2]) < $aTmp[$a][1] Then ContinueLoop Call($aTmp[$a][0]) $aTmp[$a][2] = TimerInit() Next Sleep($Sleep) Until TimerDiff($WaitTimer) >= $WaitTime If $SearchFunc <= 0 Then _ArrayDelete($g_WaitWhile_IsWaiting,UBound($g_WaitWhile_IsWaiting)-1) EndFunc Func Array2DSearch($aArray, $String, $Dimension, $StartIndex = 1, $EndIndex = "") Local $Output = -1, $a If $EndIndex = "" Then $EndIndex = UBound($aArray) - 1 If $StartIndex >= 0 And $StartIndex <= $EndIndex Then For $a = $StartIndex To $EndIndex If $aArray[$a][$Dimension] = $String Then $Output = $a ExitLoop EndIf Next EndIf Return $Output EndFunc ;==>Array2DSearch