'A continuous while loop with actions toggled on/off by a hotkey, separate from the main body. How do I approach this?

I need to have

while (true) {
   if (toggle) { ;(toggle is simply set on/off with a hotkey)
      ...stuff...
      Sleep, 250
   }
}

running independently separate from the main "thread", which in this case is stuck in a RunWait

prev_volume := 20
SoundGet, prev_volume
if (prev_volume <= 32) {
    SoundSet, 26
}
RunWait, some game
SoundSet prev_volume
ExitApp

Not sure how to approach this

Any help is appreciated, thanks in advance.



Solution 1:[1]

So to use the command SetTimer in this instance:

SetTimer, ToggleThingy, On ; defaults to 250 ms
toggle := false

prev_volume := 20
SoundGet, prev_volume
if (prev_volume <= 32) {
    SoundSet, 26
}
RunWait, some game
SoundSet prev_volume
ExitApp

{hotkey here}::
toggle := !toggle
Return


ToggleThingy:
if (toggle) { ;(toggle is simply set on/off with a hotkey)
    ...stuff...
    ; Sleep, 250 ; not needed because timer already runs every 250 ms
   }
}
Return ; needed to end the subroutine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 T_Lube