How to prevent script(s) from exitting when I change the power source of my Laptop

Get help with using AutoHotkey and its commands and hotkeys
llinfeng
Posts: 64
Joined: 08 Dec 2016, 21:54
GitHub: llinfeng

How to prevent script(s) from exitting when I change the power source of my Laptop

11 Jun 2017, 09:33

I have been using a Lenovo/Thinkpad X230T for years, and there is one thing that keeps bothering me: my AHK scripts tend to exit by itself when I switch the power source, and(or) when I switch to/from a docking station. This happens only occasionally, say 50% of the times, and applies to both scripts that I host constantly on my laptop: one "ahk_master" script that I wrote all by myself, and another ViATc script written by a veteran programmer in China.

Is there a way that I can prevent my scripts from existing? In the detailed description that follows, I will refer to this even later as "the exiting event", wherever applicable.

More details on the setup (and probably what is happening behind the scene):
Lenovo (or Thinkpad) has made a docking station specifically for X230T and I have accumulated a bunch of them over the years. It is super helpful when I switch from one workstation to another: I just grab my laptop and go. The docking process takes at most a second, and removing the laptop from the docking station take another short second. Everything in terms of the connection of the hardware and the loading of drivers are superb, except for that my AHK scripts are exiting half of the time when I move from one workstation to another.

Physically, when I unplug the laptop from a docking station, it gets detached not only from the power source, but also all other hardware connected to the docking station. And, when I "dock" the laptop, it gets connected to a (new) power source and all other connected hardware.

I have been observing the exiting event for a long time, but have not been able to identify what was causing the problem. My bet is more towards a change in power source, but I don't see why a mature scripting language should stop all its running processes due to a simple power switch.

Additional observation Aside from the exiting event related with the docking station, I have just experienced an additional set of two consecutive exiting events when I switched the power source from the battery to a portable external power bank. I have not been able to test out any other power bank yet.
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

11 Jun 2017, 09:40

Do you notice any other programs are being shut down? I wonder if it's a matter of going onto battery that your computer is doing some " 'Smart' Power Management " by shutting down programs, and AHK is meeting its criteria for what ones to shut down - maybe those that are infrequently using CPU (so hotkeys that are on timers for like once every 30 minutes or longer, or hotkeys not often used). It could be that even just one infrequently used script results in this power manager shutting down all AHK scripts blindly.

Consider making an OnExit command and subsequent routine for it to run. You might even be able to glean some information, like what is trying to shut down the script, though I do not know the specifics on how you would. What I would do is use a MsgBox with a Yes/No that asks "Do you want to shut down the script %A_ScriptName%?" and then use IfMsgBox Yes/No to decide what course to take. (Yes would use ExitApp, No would use return and ignore the exiting.)
llinfeng
Posts: 64
Joined: 08 Dec 2016, 21:54
GitHub: llinfeng

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

11 Jun 2017, 09:53

@Exaskryz, nothing else is existing, it is only the AHK scripts(both of them). Will try out what you suggested. Should I add a section towards the end of both of the scripts to have the script monitor what is going wrong when exiting? (Please correct me if I got you wrong.)
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

11 Jun 2017, 10:16

As long as the script executes an OnExit command (usually found in the auto-execute section), feel free to do whatever you'd like with it.
llinfeng
Posts: 64
Joined: 08 Dec 2016, 21:54
GitHub: llinfeng

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

11 Jun 2017, 18:53

Exaskryz wrote:As long as the script executes an OnExit command (usually found in the auto-execute section), feel free to do whatever you'd like with it.
While having a message box conditional on a (normal) exit sounds reasonable, I wonder if "#Persistent" may solve my problem in a persistent manner? I read about it in the help file for OnExit command, and the description looks promising:
Help Doc wrote:The following examples use #Persistent to prevent the script from exiting automatically.
Real 3L0
Posts: 2
Joined: 26 Oct 2017, 10:55

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

02 Jul 2018, 05:52

llinfeng wrote: While having a message box conditional on a (normal) exit sounds reasonable, I wonder if "#Persistent" may solve my problem in a persistent manner? I read about it in the help file for OnExit command, and the description looks promising:
Help Doc wrote:The following examples use #Persistent to prevent the script from exiting automatically.
You solve problem?
llinfeng
Posts: 64
Joined: 08 Dec 2016, 21:54
GitHub: llinfeng

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

12 Aug 2018, 16:39

Real 3L0 wrote:
llinfeng wrote: While having a message box conditional on a (normal) exit sounds reasonable, I wonder if "#Persistent" may solve my problem in a persistent manner? I read about it in the help file for OnExit command, and the description looks promising:
Help Doc wrote:The following examples use #Persistent to prevent the script from exiting automatically.
You solve problem?
Apologize for the late reply. Yes, I have solved the problem. The complete story is a bit long, and I can feed in more details upon request.

In short, my AHK scripts are existing due to my own "Windows Setting": I used Task Scheduler to execute AHK in the background every time that I start Windows (or log on). It was one of the default options in Task Scheduler that "killed" the AHK scripts when I swap the power source. This dangerous option goes in the following screen shot. Switching these options off get me an AHK script that is always running, however I swapped the power source.
AHK_Scheduler_Setting.jpg
AHK_Scheduler_Setting.jpg (170.78 KiB) Viewed 1296 times
User avatar
geekyadam
Posts: 33
Joined: 01 Aug 2016, 17:11

Re: How to prevent script(s) from exitting when I change the power source of my Laptop

30 Jan 2020, 10:20

Sorry to necro old threads but I wanted to add something for anyone else experiencing similar behavior that might help them...
Theoretically a docking station or USB hub could have a USB flash drive or external drive attached, and it could be possible that AHK was installed to that location. If you disconnect from the docking station, any running AHK scripts could have issues running and/or crash because the connection to the filesystem where AHK is installed was severed. Maybe AHK is loaded into memory upon initial execution, but not sure what would happen if system went to sleep mode and back if there would be issues or something like that. Just thought I'd mention it here in case it was helpful to someone.
I thought about this because I used to keep a flash drive with portable version of AHK installed with some helpful debug/IT-related scripts on it. So I could plug into any Windows PC and run the scripts natively from the flash drive. Helps when you're consistently debugging friends/family PCs or moving large files from one system to another etc.

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, Google [Bot], ingewll and 47 guests