How to Prevent a Macro From Running Twice at the Same Time?

Occasionally I misremember my hotkeys and trigger the same macro again instead of hitting a keystroke assigned to a pause until in that macro. Anyway to prevent the second instance from starting if the first one is already active?

Yep. Use a Semaphore Lock action.

I noticed that the question in your topic title is not the same as the one in your post:

(1) How to prevent a macro from running twice at the same time?

(2) Anyway to prevent the second instance from starting if the first one is already active?‌

‌‌
For question (1) the default Semaphore Lock is sufficient: The triggered macro instances will be queued, but each one will run eventually. (One after the other.)

For question (2) you need either…

  • a Semaphore lock with a very short timeout, as explained in point 4 of this section of the Semaphore Lock KM Wiki article, or…
  • a Disable Macro action at the beginning and an Enable Macro action at the end of the macro.

Here are four minimal examples to demonstrate it:

The core actions of each macro are…

  • a pause of 5 seconds
  • a Speak Text action “Action Completed”

To get a picture of the differences, launch each macro with its hotkey trigger, then press the hotkey again 4 times during the 5-seconds pause. (Or more often if you like.)

Normal macro without anything special

10-pty-fs8
[mwe] Pure Macro (Without Anything).kmmacros (2.0 KB)

:arrow_forward: After 5 seconds you will hear “Action Completed” 5 times, almost at the same time (depending on your speed when you were pressing the hotkey ×5).

With Semaphore Lock (default settings)

55-pty-fs8
[mwe] Semaphore Lock (Default).kmmacros (2.3 KB)

:arrow_forward: After 5 seconds you will hear “Action Completed” 5 times, in intervals of 5 seconds.

With Semaphore Lock (very short timeout)

16-pty-fs8
[mwe] Semaphore Lock (with Very Short Timeout).kmmacros (2.4 KB)

:arrow_forward: After 5 seconds you will hear “Action Completed” once.

With Disable/Enable Macro actions

21-pty-fs8
[mwe] Disable:Enable Macro.kmmacros (2.5 KB)

:arrow_forward: After 5 seconds you will hear “Action Completed” once.



Disclaimer: I’m not using Semaphore Locks very often, so it is quite possible that I’m missing some details.

6 Likes

This is super helpful and educational, you're completely correct on the title, thanks so much!