Jump to content

RegisterLoop + WaitWhile - run multiple loops with "sleep" without stopping the other loops


Guest
 Share

Recommended Posts

About the title, it's not accurate .. I could not to think about a better title.

The method I suggest here is not multithreading,

but I call it "multilooping" Because it looks like this (Maybe technically it incorrect but that's not the point)

I guess that most people want multithreading because they want to use sleep() but just to affect some part of the

code and not the whole script.

So what I suggest here is a way to do that but note that this is a FAKE sleep (Otherwise it would not work).

But the final point of view we achieve what we want - Which is act of waiting.

This is the example:

#include 'RegisterLoop.au3'


; Press Exit to exit the script
HotKeySet('{ESC}','Exit1')
Func Exit1()
    Exit
EndFunc


Global $cLoop2 = 1

; Register loop1 - this will cause to function 'loop1' to run every 50 ms
RegisterLoop('loop1',50)

; Register loop2 - this will cause to function 'loop1' to run every 250 ms
RegisterLoop('loop2')

; RegisterLoop('loop3') ;You can add this line if you want. But it will cause more confusion



Func loop1()
    ToolTip('loop1')
EndFunc



Func loop2()
    ConsoleWrite('loop2 ('&$cLoop2&')'&@CRLF)
    $cLoop2 += 1
    WaitWhile('loop2',5000) ; The magic begins here. The entire system was developed just for this -
    ; the ability to "Sleep" in function that registered by AdlibRegister() ***without freezing other functions that registered
    ; by AdlibRegister()***.

    ; If you register function with AdlibRegister(), you have a limitation that if the function will sleep, it will cause the entire script
    ; to sleep with the function.

    ; but if you use WaitWhile(), it will look like it is sleeping (but it is not..) and the other code outsite the function will continume
    ; to run almost the same speed.

    ; As you can see, If you move the mouse, you will see that ToolTip('loop1') not sleeping ___ 5000ms while this function sleeping 5000ms


    ; If You replace WaitWhile('loop2',5000) with the line below:
    ;Sleep(5000)

    ; then ToolTip('loop1') will sleep 5000ms because it is affected by this function.

EndFunc






Func Loop3()
    ConsoleWrite('loop3'&@CRLF)
    WaitWhile('loop3',3000)
EndFunc









; Main loop just to keep the script running
While 1
    Sleep(10000)
WEnd

And this is the UDF:

RegisterLoop_v2.au3

RegisterLoop.au3

 

Enjoy

Edited by Guest
Link to comment
Share on other sites

Please note,
At this point it is limited  only to one "Sleep" at time.

If you "Sleep" in more then one function like in this example:

Func loop1()
    ToolTip('loop1')
    WaitWhile('loop1',1000)
EndFunc

Func loop2()
    ConsoleWrite('loop2 ('&$cLoop2&')'&@CRLF)
    $cLoop2 += 1
    WaitWhile('loop2',5000) ; The magic begins here. The entire system was developed just for this -
EndFunc

Then it will break the "Sleep" in the other function..

I'm still working on how to solve it ..
This is something that makes it much more complicated
.

EDIT:

Problem solved :)

Now you can bypass this limitation also with RegisterLoop_v2.au3

Download it from the first post

 

Please let me know if you found any bug.
It has not been tested thoroughly.

 

EDIT:

Note that,
There is still one Limitation
when you "Sleep" (WaitWhile) more the once at the same time. It is hard for me to explain in English.

But you should see it in this example:

Func loop1()
    ToolTip('loop1')
    WaitWhile('loop1',3000)
EndFunc



Func loop2()
    ConsoleWrite('loop2 ('&$cLoop2&')'&@CRLF)
    $cLoop2 += 1
    WaitWhile('loop2',5000)
    ConsoleWrite('Ended' &" (Line "&@ScriptLineNumber&")"&@CRLF&@CRLF) ; <- It does not end at the right time. It ends after abput 1000 ms delay
;        Because WaitWhile('loop1',3000) in function loop1()
EndFunc

EDIT:

I was worng before. the limitation above is less critical.

With this analysis

Func loop2()
    Local $timer = TimerInit()
    ConsoleWrite('loop2 ('&$cLoop2&')'&@CRLF)
    $cLoop2 += 1
    WaitWhile('loop2',5000)
    ConsoleWrite('Ended - '&TimerDiff($timer) &@CRLF&@CRLF)
EndFunc

It does not end after 3000ms delay. it ends after about up to 1100ms delay.

This is my result:

 

loop2 (1)
Ended - 5029.10772411375

loop2 (2)
Ended - 6178.25853672354

loop2 (3)
Ended - 6163.09792164511

loop2 (4)
Ended - 6193.14410137948

loop2 (5)
Ended - 5004.13930359696

 

EDIT:

But when loop2() need to start again, it start after 3000 ms delay. this is the problem. it should start again at that moment it was ended.

Edited by Guest
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...