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:
- Export the task as an XML file.
- Edit the XML file and change the Priority tag. See this page for a priority list.
- Set the StopOnIdleEnd to "false". (Haven't tested this, but it might have been causing mouse/input lagging.)
- 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.