Share via:

Infinite Loop in AutoIt

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


Edited: 2017-03-18 04:45

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

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

Func MyExit()

Be careful

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

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

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