r/AutoHotkey•
msx92

AHK script disappears after sleep/hibernate?

Need Help

Hi, I have a script for multimedia controls that I run on startup via task scheduler ("every time a user logs in"). Unfortunately, when my PC goes into sleep/hibernate the script disappears from the right side of the task bar and stops working so I have to manually launch it again.

Is there any way to make the script persist through sleep/hibernate or to automatically relaunch it when waking up?

Posted Mar 14, 2022, 5:00 PM UTC

27
NonCombat•

Don't hibernate.

But not that I'm aware off, it's odd that it's even closing it. Never had that.

1
plankoe•

I use this my own script to reload it when I log in.

SessionChange(true)
WhenUserLogsIn()
{
    Reload
}

SessionChange(notify := true)
{
    static WTS_CURRENT_SERVER      := 0
    static NOTIFY_FOR_ALL_SESSIONS := 1

    if (notify)                                                                   ; http://msdn.com/library/bb530723(vs.85,en-us)
    {
        if !(DllCall("wtsapi32.dll\WTSRegisterSessionNotificationEx", "Ptr", WTS_CURRENT_SERVER, "Ptr", A_ScriptHwnd, "UInt", NOTIFY_FOR_ALL_SESSIONS))
            return false
        OnMessage(0x02B1, "WM_WTSSESSION_CHANGE")
    }
    else                                                                          ; http://msdn.com/library/bb530724(vs.85,en-us)
    {
        OnMessage(0x02B1, "")
        if !(DllCall("wtsapi32.dll\WTSUnRegisterSessionNotificationEx", "Ptr", WTS_CURRENT_SERVER, "Ptr", A_ScriptHwnd))
            return false
    }
    return true
}

WM_WTSSESSION_CHANGE(wParam, lParam)                                              ; http://msdn.com/library/aa383828(vs.85,en-us)
{
    static WTS_SESSION_UNLOCK := 0x8
    if (wParam = WTS_SESSION_UNLOCK)
		WhenUserLogsIn()
}
1
msx92OP•

My script gets automatically loaded on startup and only disappears when the PC goes into sleep mode. The next time I restart the PC fully it's there again. Do you think this script would change that?

1
plankoe•

Try putting #Persistent at the top of your script. The script is not supposed to disappear when the PC goes to sleep, but this might help. The script I provided only reloads the script after logging in. It doesn't start up the script.

1
jcunews1•

Is your script does something using timer, on a window message, or on other event?

1
msx92OP•

My script is really simple it just has 6 lines mapping volume up,down,mute,play/pause, next, previous to alt + arrow keys/spacebar/"m"

1
jcunews1•

Windows preserves all running applications when the computer woke up from sleep state or came from hibernation. It shouldn't affect applications which don't do something that may be affected by the event. You might want to try updating AHK. If the problem persist, chances are that, there's inteference from other (faulty) application.

1