A thread looping in background Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
wKey
Posts: 7
Joined: 08 Jul 2019, 05:08

A thread looping in background

Post by wKey » 03 Jun 2023, 23:05

I'm drafting a script to do something like this :

Code: Select all

global list := []

F1:: {
list.Push(A_Clipboard) ; get info from clipboard, then append to the list
if (list.Length > 1)
 SetTimer do_stuff(), 0 ; launch once
}

do_stuff() {
Loop
 {... break ...} ; break if there's no item left in the list
Exit
}
What I can't figure out is how to make the do_stuff function keep working in background while the hotkey F1 is available to push new item into the list.

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: A thread looping in background  Topic is solved

Post by mikeyww » 04 Jun 2023, 06:00

Ideas are below.

Code: Select all

#Requires AutoHotkey v2.0
list    := []
running := False

F1:: {
 list.Push(A_Clipboard)
 SoundBeep 2500
 If list.Length > 1 && !running {
  SetTimer do, 3000
  Global running := True
  ToolTip 'Running'
 }
}

do() {
 Send list.RemoveAt(1)
 If list.Length < 2 {
  SetTimer do, 0
  Global running := False
  ToolTip
 }
}
Concepts:
1. To enable a timer, use a frequency greater than zero.
2. A loop can be used in a timed routine, but if the idea is to execute code at a timed frequency, then Loop is omitted because SetTimer creates a timed loop.
3. AHK does not directly provide a way to know whether a timer is running, but you can use a variable to track that information.
4. The documentation can help you learn more about how to use SetTimer. See the examples.
5. > means "greater than", rather than "greater than or equal to". Change the operators if needed. The documentation provides a complete listing of valid operators.

wKey
Posts: 7
Joined: 08 Jul 2019, 05:08

Re: A thread looping in background

Post by wKey » 04 Jun 2023, 06:11

@mikeyww : thanks for your reply
I've just made it work by putting all the code into multiple SetTimer then add Thread "Interrupt", 0 .

Post Reply

Return to “Ask for Help (v2)”