Infinite Loop in AutoIt

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

4860 views

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
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 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
WEnd

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

Tell us what you think:

  1. How to declare and work with variables in AutoIt, as well as some background information.
  2. Everything you need to know about working with minimized windows.
  3. How to set the request headers when performing HTTP requests.
  4. This Tutorial will focus on post requests in AutoIt, using the Winhttprequest.5.1 object.
  5. How to use while, for, and do until loops, and how to loop through arrays and object in AutoIt.

More in: AutoIt Tutorials