Sign in to follow this  
Followers 0
Radish

Please Explain How While Wend Infinite Loop Works

5 posts in this topic

Hi,

I fundamentally don't know how this While... Wend loop works in relation to other code in the script - could someone please provide an explanation.

Local $aPos[2] ;Declaring an array to hold mouse coordinates.

HotKeySet("!m", "GetMouseCoords") ;Capture the current mouse coordinates <Alt>+m.
HotKeySet("!{SPACE}", "SaveToFile") ;Start the 'Save File' function <Alt>+<Space>.
HotKeySet("!{ESC}", "Terminate") ;Exit from the script/program <Alt>+<Esc>.


While 1 ;This keeps the script permanently running until 'exit' hotkey pressed.
    Sleep(100)
WEnd

Func GetMouseCoords()
    $aPos = MouseGetPos() ;Capture the current mouse coordinates.
EndFunc   ;==>GetMouseCoords

Func SaveToFile()
    MouseMove($aPos[0], $aPos[1]) ;Moves the mouse to the previously captured mouse coordinates.
    ; <Other lines of code would follow here.>
EndFunc   ;==>SaveToFile

Func Terminate()
    Exit ;Exit the script/progam.
EndFunc   ;==>Terminate

Where I have the problem is that, as far as know - based on what I've read in the AutoIt help file - the above While... Wend loop is an infinite loop. My understanding of an 'infinite loop' is that the loop is entered in the functioning program and there the program gets 'stuck'/'locked in that loop'. Yet this can't be what is happening here - after all the program still monitors the keyboard for HotKey presses, and, if one of the hotkeys is pressed, then the program (correctly) goes to one of the three functions and (correctly) executes whatever instructions are contained inside the function. How can the program do this if it is stuck in an infinite loop - should it not just be perpetually looping in the loop? How does it (seemingly) jump of the loop to check the keyboard and then jump back in the loop to keep the program perpetually running (till the 'exit' key is pressed)?

(I hope I've explained the problem well enough for people to get a handle on what I'm trying to say. I'm really asking a 'concept question' I suppose - I don't understand how the program does anything other than stay in the loop. I also suspect that it is 'staying in that loop' that allows the program to constantly run monitoring for key-presses - instead of just on launching the program the program runs for a split second then just exits from itself.)


Operating System: Windows 7 Pro. x64 SP1

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't know the technicals of how autoit works, but in that instance... hotkeyset is acting outside of the main code.

It will monitor your key presses regardless of what the code is doing. The while loop itself, keeps the program running, so that the hotkeys will continue to be monitored.

Edited by Sori

If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

The program is like a sheet of paper. It reads all lines till its done then closes. Period.

 

Now by the time you get to the end you have < 1 second and your program closes. So to keep it open, people use a endless loop to keep it open indefinitely.

 

Now of course its not in a responsive less loop, every iteration of that loop autoit does a check for hotkeys and functions and everything else. So even though your in a endless loop, autoit was designed so that every iteration of a loop, it checks external loop functions and calls.

Now your hotkeyset is a function call, so your program works like this.

It runs, goes into a loop. Gets to the end of the loop, does a 'health' check and discovers the user pressed ESC. It now then goes into that function and runs the code. When that function is done and returns, it checks for anymore calls. If there is nothing left, the health check is done. Autoit then goes back to the original loop and continues the next run of it.

From my understanding Autoit has many 'help' features. Like loops can still check for code, you can always pause the script, ctrl/alt/delete can free a stuck script etc. It has features to ensure scripts dont go rogue, and they were purposely put in to help people. Its a scripting language and what we execute is always monitored so it can provide these help features to us.

 

Now, in most programming languages that endless loop is a disaster yes, however this is autoit and its designed to be easy, logical and have little tricks like this so you can use them to your advantage. It also makes endless loops have a purpose instead of being a problem. Does this help?

Edited by sherkas

Share this post


Link to post
Share on other sites

Okay, Sherkas, thanks for the clear and full response. I see where my 'conceptual thinking' is wrong now. My only previous experience of programing comes from BASIC and there if you went into an infinite loop - that was it, that was where you stayed. So my conceptual difficulty lay not with understanding an infinite loop (which I thought was my problem) but with understanding an aspect of the way AutoIt functions.

Seems to me that the conceptual information you just posted should really be in the AutoIt Help File - 'A Note on How Infinite Loops are Treated in AutoIt'. That such information isn't already in the Help File is a bit of loss, considering how fundamental that concept is to using AutoIt. (I'm not saying this by way of criticism of the Help File - I'm saying it to be constructively helpful.)

Many thanks, again. (Will mark the thread as 'solved' now.)


Operating System: Windows 7 Pro. x64 SP1

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Radish,

Hi and welcome to AutoIt!  Glad you got the confusion cleared up.  I see that you are relatively new to AutoIt so let me make the following points for your benefit:

1 - The Help file is NOT a programming tutorial, it is a reference guide for the language.  With that said, a close reading of

     the  Help file tells you  much about the way the language behaves, from the Help file for "HotKeySet":

A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes or is interrupted. Exceptions are as follows:
1) If the current function is a "blocking" function, then the key-presses are buffered and execute as soon as the blocking function completes. MsgBox() and FileSelectFolder() are examples of blocking functions. Try the behavior of Shift-Alt-d in the Example.
2) If you have paused the script by clicking on the AutoIt Tray icon, any hotkeys pressed during this paused state are ignored

 

2 - Technique / method questions can be answered in the forums.  As you have seen the members are very helpful.

3 - There are several tutorials on the Wiki.

Looking forward to working with you,

kylomas

edit: trying to straighten out the formatting...

Edited 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

Share this post


Link to post
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
Sign in to follow this  
Followers 0