Using SetTimer with a small delay is difficult to interrupt. Why?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Doogs
Posts: 2
Joined: 14 Oct 2018, 20:57

Using SetTimer with a small delay is difficult to interrupt. Why?

14 Oct 2018, 21:44

Spamming a key too fast seems to be difficult to interrupt. My code essentially reduces to this:

Code: Select all

SpamKey()
{
  Send *
  delay := 20
  SetTimer, SpamKey, %delay%
}

q::
  SetTimer, SpamKey, Delete
Return

s::
  SpamKey()
Return
The output is something like this:

Code: Select all

**********Q****************************Q*************
where Q (why is it capital anyway??) are the times I've tried to break the spam. The same behaviour occurs if I implement using the Sleep command. Why is this the case? Also I have another spam routine that acts like this:

Code: Select all

g_isKeyPress := True

Spam()
{
  global g_isKeyPress
  g_isKeyPress := !g_isKeyPress
  if (g_isKeyPress)
  {
    Send *
    funct := Func("Spam").Bind(False)
    SetTimer, %funct%, -20
  }
  else
  {
    funct := Func("Spam").Bind(True)
    SetTimer, %funct%, -600
  } 
}

q::
  SetTimer, Spam, Delete
Return

s::
  Spam()
Return
It essentially Sends a key input, waits 20ms, waits a further 600ms, and repeats. This routine is far easier to interrupt, but still sometimes fails to resister 'q' to break the timer. It seems to be if a timer has a small delay (here it is 20ms, but I've also noticed this with a delay of 50ms) it is difficult to interrupt. Why is this and what can I do for a workaround? Workarounds I've tried without success:
- Using the sleep command
- Breaking by using a global flag and checking the flag in the spamming routine
Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Using SetTimer with a small delay is difficult to interrupt. Why?

15 Oct 2018, 01:34

Hallo,
Why Q ? To send *, the timer must press Shift. If you press q at this moment, you press Shift + q and write Q because Q is not a Hotkey.
Replace q:: by *q:: or q Up::
eelrod
Posts: 65
Joined: 10 Apr 2018, 11:17

Re: Using SetTimer with a small delay is difficult to interrupt. Why?

15 Oct 2018, 07:55

While it might be because the delays are short, I suspect that it might be due to how you're calling the timer. I would suggest that, instead of calling your timer within the function that ends up being your timer, to call it from the hotkey itself. So, per your first example, something like this:

Code: Select all

SpamKey()
{
	Send , *
}

q::
SetTimer , SpamKey , Delete
Return

s::
delay := 20
SetTimer , SpamKey , %delay%
Return
As for why it was a capital Q, I suspect Rohwedder is correct.
Doogs
Posts: 2
Joined: 14 Oct 2018, 20:57

Re: Using SetTimer with a small delay is difficult to interrupt. Why?

15 Oct 2018, 19:44

Thank you for your responses. Rohwedder you were right with the * requiring the shift key. This indeed fixed the example but the problem still existed in my code though. I managed to find a solution by setting a global flag and allowing the thread to run it's course. Flagging a thread to end seems to be less error prone than trying to delete it, and really this strikes me as the standard approach to end threads.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, Rohwedder, RussF and 321 guests