I've been trying to auto-start some Auto-HotKey scripts using the Task Scheduler since it allows the scripts to run with admin privileges without a UAC prompt.

Usually, the scripts fail after several hours. When run via UAC/explorer.exe/startup folder, all these scripts have run for several days. (The only one I've seen work reliably via Task Scheduler is a simple one that pops-up a "Hello World" message box.)

By using Process Explorer, I notice that taskeng.exe child processes always had their Working Sets reduced in size (from ~3,000K to 512K or so), while the UAC/explorer.exe child processes remained constant.

I tried using some AHK code to see if it could change the Working Set size:

pid := DllCall("GetCurrentProcessId")
handle := DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", pid)
DllCall("SetProcessWorkingSetSize", "UInt", handle, "Int", 10000000, "Int", 20000000)
DllCall("CloseHandle", "Int", handle)

It didn't seem tow work, so I'm unsure if there is any fix to the problem.

The solution was to compile the executable as 64-bit instead of 32-bit. I hadn't tried this previously because the 32-bit version worked perfectly fine via the Startup folder over the months I used it.

I believe the problem stemmed from the keyboard-hook being loaded into memory and then unloaded during garbage collection. Only keyboard-hook assigned hotkeys were affected, not ones assigned using RegisterHotkey.

I've been using the 64-bit executables for over a week now and none have failed, though some have been a little slow to respond (might have something to do with background processes or Chrome eating up resources). Killing off or restarting the parent taskeng.exe and svchost processes might fix this problem since that was the only way I found to stop the Working Set reallocation (still didn't fix the 32-bit problem and killing the parent svchost created some new problems).

[2018-10-03 Update: I've basically solved the problem.] Task Scheduler can set tasks to run at different priorities, but the user-interface doesn't show it.

To change the priority:

  1. Export the task as an XML file.
  2. Edit the XML file and change the Priority tag. See this page for a priority list.
  3. Set the StopOnIdleEnd to "false". (Haven't tested this, but it might have been causing mouse/input lagging.)
  4. Import the modified XML file as a task and it should work.

Raising the priority this way also increases memory and I/O priority.

Compiling AHK scripts as 64-bit may still be required.

  • I also use Task Scheduler to start AutoHotkey on startup and face same problem. I have also noticed it happens when I run it manually too. Not sure why. As a workaround, I have added run as admin shortcut for it in start menu and run it from there whenever it crashes – VarunAgw Apr 1 '18 at 11:33
  • What's strange is that I ran a large/complex, many-key script for many months without problems (via Start Menu with no elevation to admin). It worked perfectly fine, but now it's having problems maintaining keybindings. Either I'm running too many scripts, elevating to admin causes problems, or locking my account is causing it. – RedLetter Apr 10 '18 at 22:22
  • Forgot to add that I'm also launching via an admin shortcut as well (with a script launcher to reduce clicks) and still have those problems I mentioned. I think screen locking might actually be a problem, but I'm still testing things it out (it wasn't a problem when I ran via the Startup folder). – RedLetter Apr 10 '18 at 22:29
  • Screen locking is not a problem (at least for me it happens at random time while working). My AHK script is 2K line long. Finding the issue in it is like finding a needle in a haystack esp since it happens very rarely. If you really want, you can try splitting it in small scripts and see which part crashes – VarunAgw Apr 11 '18 at 8:19
  • Also see this. Maybe it help you. – VarunAgw Apr 11 '18 at 8:22

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.