How to determine if a hotkey or hotstring, etc is currently running

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

How to determine if a hotkey or hotstring, etc is currently running

07 Jan 2024, 12:22

I am going to be writing a small script with a timer to reload a few scripts. One of the scripts is heavily used so I'd like to be able to know if it is running a hotkey or hotstring, whatever. If so, it will wait till done (and\or determine that it's done and is in a dormant state) before reloading the script. I searched but I only found information regarding exe's. Is this possible? Thanks in advance.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: How to determine if a hotkey or hotstring, etc is currently running

07 Jan 2024, 12:36

A few ideas are below.

1. Send a string of any length from one script to another.

2. IniWrite can be used to log or track status or any other type of information.

3. Combine your scripts so that you do not have to bother with any of this.

4. Use the main script itself to determine when it should reload itself.
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to determine if a hotkey or hotstring, etc is currently running

07 Jan 2024, 13:01

Thank you very much Mikeyww, i'll look into those.
Descolada
Posts: 1202
Joined: 23 Dec 2021, 02:30

Re: How to determine if a hotkey or hotstring, etc is currently running

07 Jan 2024, 17:10

Another option is using a mutex or semaphore object to signal this kind of state.

Example using two separate scripts:

Waiter.ahk, the script that checks whether the other script is doing something important and waits for it to finish first.

Code: Select all

#Requires AutoHotkey v1

mtx := new Mutex("Local\MyMutex")
if mtx.Signal() = 0
    mtx.Release()
MsgBox Unblocked!

class Mutex {
    /*
     * Creates a new Mutex. 
     * @param name The name can start with "Local\" to be session-local, or "Global\" to be 
     * available system-wide.
    */
    __New(name) {
        if !(this.handle := DllCall("CreateMutex", "ptr", 0, "int", 0, "str", name))
            throw Exception("Unable to create or open the mutex", -1)
    }
    /* Tries to signal ("reserve") the mutex within the timeout period.
     * @param timeout The timeout period in milliseconds (default is infinite wait)
     * @returns {Integer}
     * 0          = successful
     * 0x80       = abandoned
     * 0x120      = timeout
     * 0xFFFFFFFF = failed
     */
    Signal(timeout:=0xFFFFFFFF) {
        return DllCall("WaitForSingleObject", "ptr", this.handle, "int", timeout, "int")
    }
    ; Releases the mutex (resets it back to the unsignaled state)
    Release() {
        return DllCall("ReleaseMutex", "ptr", this.handle)
    }
    __Delete() {
        DllCall("CloseHandle", "ptr", this.handle)
    }
}

/*
 * Waits for multiple objects to be signaled.
 * @param objArray An array of object handles, or AHK objects with ptr or handle properties
 * @param waitAll If 1 then waits for all objects to be signaled, if 0 then at least one.
 * @param timeout The timeout period in milliseconds (default is infinite wait)
 */
WaitForMultipleObjects(objArray, waitAll:=1, timeout:=0xFFFFFFFF) {
    VarSetCapacity(buf, objArray.MaxIndex()*A_PtrSize, 0)
    for i, obj in objArray
        NumPut(IsObject(obj) ? (obj.HasKey("ptr") ? obj.ptr : (obj.HasKey("handle") ? obj.handle : obj)) : obj, buf, (i-1)*A_PtrSize, "ptr")
    return DllCall("WaitForMultipleObjects", "int", objArray.MaxIndex(), "ptr", &buf, "int", !!waitAll, "int", timeout)
}
Blocker.ahk, the one that should cause the other script to wait:

Code: Select all

#Requires AutoHotkey v1

mtx := new Mutex("Local\MyMutex")
if (mtx.Signal() = 0)
    MsgBox I am now blocking the Mutex until this MsgBox is closed
Else
    MsgBox Signaling the Mutex failed
mtx.Release()

class Mutex {
    /*
     * Creates a new Mutex. 
     * @param name The name can start with "Local\" to be session-local, or "Global\" to be 
     * available system-wide.
    */
    __New(name) {
        if !(this.handle := DllCall("CreateMutex", "ptr", 0, "int", 0, "str", name))
            throw Exception("Unable to create or open the mutex", -1)
    }
    /* Tries to signal ("reserve") the mutex within the timeout period.
     * @param timeout The timeout period in milliseconds (default is infinite wait)
     * @returns {Integer}
     * 0          = successful
     * 0x80       = abandoned
     * 0x120      = timeout
     * 0xFFFFFFFF = failed
     */
    Signal(timeout:=0xFFFFFFFF) {
        return DllCall("WaitForSingleObject", "ptr", this.handle, "int", timeout, "int")
    }
    ; Releases the mutex (resets it back to the unsignaled state)
    Release() {
        return DllCall("ReleaseMutex", "ptr", this.handle)
    }
    __Delete() {
        DllCall("CloseHandle", "ptr", this.handle)
    }
}
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Re: How to determine if a hotkey or hotstring, etc is currently running

09 Jan 2024, 10:28

@Descolada Thank you so much, I will definitely try this out. Thanks for your time, I appreciate it!

Kelly

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Hansielein, Lpanatt and 328 guests