By using this site you agree to the use of cookies by Brugbart and our partners.

Learn more

Infinite Loop in AutoIt

This autoit tutorial shows how to create an infinite loop, and how to make an exit function.

Edited: 2013-08-01 10:36

Creating an infinite loop in AutoIt is quite easy, you will simply have to leave out the counter of the loop. The easiest way to create a loop that never stops, and just continues running, is likely to use the While loop, we will be looking into that in this Tutorial.

An infinite loop can be made with the while keyword, like demonstrated in the below example.

While 1
  ; do stuff here
WEnd

Creating an Exit function

Normally you want a way to stop the script, without having to kill it from task manager, such as exiting when hitting a hotkey. To do this we simply create a custom function, and make it possible to call it using the CTRL+ALT+x key combination. I.e.

HotKeySet("^!x", "MyExit")

While 1
  ; do stuff here
WEnd

Func MyExit()
    Exit 
EndFunc 

Be careful

Infinite loops could potentially end up taking up a lot of memory, this can happen if accidentally placing a variable that is slowly filling up while the script is running. I.e.

While 1
    ; counter accidentally left in the script
    $counter = $counter + 1
WEnd

Many beginners make this mistake – or variations of it – because a lot of examples are including the counter.