trying to prevent a function from running more than once at a time

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: trying to prevent a function from running more than once at a time

28 Mar 2019, 20:57

NewberEnerNeeder1 wrote:
28 Mar 2019, 19:23
Yes this is when a hotkey is already assigned to something else.

The easiet way to make a hotkey bypass all previous hotkeys assigned to f1 for example would be something like this:

Code: Select all

#if ; Add Context-sensitive hotkeys below this #if - they will always be active and overpass previously assigned ones
F1::
Msgbox Hi, this hotkey triggers the msgbox EVERYWHERE no matter what program has the focus.
return
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: trying to prevent a function from running more than once at a time

29 Mar 2019, 06:03

i would like to be able to press f1 to start the function and if i press f1 again while it is running the f1 will not start it a 2nd time
This is what your existing code does. A hotkey will not fire if the thread for the previous fire of that hotkey has not finished yet.
So the problem is not what you think it is, it's something else.
ie the sending variable in your script has absolutely zero effect, and could not have any effect unless you had used #MaxThreadsperHotkey 2, which you obviously would not want to do.
Proof of what I am saying:

Code: Select all

F12::
Tooltip % A_TickCount
Sleep 5000
return
Hit F12 once, tooltip appears with current tick count. If you hit it again within 5 seconds, nothing will happen, because the previous hotkey thread has not finished as it is processing the line Sleep 5000
NewberEnerNeeder1
Posts: 47
Joined: 26 Mar 2019, 06:06

Re: trying to prevent a function from running more than once at a time

29 Mar 2019, 18:45

evilC wrote:
29 Mar 2019, 06:03
i would like to be able to press f1 to start the function and if i press f1 again while it is running the f1 will not start it a 2nd time
This is what your existing code does. A hotkey will not fire if the thread for the previous fire of that hotkey has not finished yet.
So the problem is not what you think it is, it's something else.
ie the sending variable in your script has absolutely zero effect, and could not have any effect unless you had used #MaxThreadsperHotkey 2, which you obviously would not want to do.
Proof of what I am saying:

Code: Select all

F12::
Tooltip % A_TickCount
Sleep 5000
return
Hit F12 once, tooltip appears with current tick count. If you hit it again within 5 seconds, nothing will happen, because the previous hotkey thread has not finished as it is processing the line Sleep 5000
ic ty for the insight on this

i thought there might be some type of easy fix for this like maybe i did some type of oversight but it seems to be a bit harder than i first thought.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], wilkster and 295 guests