When launching a script at windows startup via task scheduler, there is a problem where Windows treats it as a background task and the script's process receives lower CPU and memory priorities, which affects performance of the script significantly. Any other processes run by it also receive these lower priorities and will run slow as well. The values for CPU and memory can be exposed in Process Explorer:

The default priorities appear to be 8 for CPU and 5 for memory. Task scheduler gives them lower values like 6 and 3.

Process, Priority, ,N restores the CPU priority to 8, but memory priority is still 3 and performance of the script and any programs launched by the script is noticeably slower (eg. Chrome launch via URL link in my script GUI takes approximately 5 seconds; seems Chrome doesn't like the low memory priority of 3).

I found the priorities can be increased to 8 and 4 by creating the task with XML file <Priority>4</Priority> , however memory priority is still lower (4 instead of 5). Performance seems ok, but still I want the exact same priorities as if the user was just run the program normally as my script does some memory intensive things occasionally.

According to this it should be possible to do with a dllcall. Here is my unsuccessful attempt:

According to the SetPriorityClass function I need to have PROCESS_SET_INFORMATION privileges, which I can't seem to find a way of doing. Can anyone help?

Also I tried the command line processPriority.exe by user Charlie in that thread, but it returns error "unable to open process".

I've tried Reload aswell but it doesn't change the priorities. SingleInstance Off and then launch a new parallel instance should work, but then the UAC nag for admin privileges will pop up which defeats the whole purpose of using task scheduler in the first place, as it's the only way to silently run as admin at windows startup.

Thanks

edit:<Priority>2</Priority> in the task scheduler XML is now giving me the default 8 and 5 values. However that's not how the documentation says it's supposed to work, which leads me to believe it's going to result in above normal values on other systems. Perhaps <Priority>2</Priority> in combination with Process, Priority, ,N will yield a safer result.

I tried getting the SeDebugPrivilege per the dll example in the Process documentation, but it's not working. The whole access rights API is way over my head and I would need to take at least a 3 day seminar to understand it

In the meantime here is how I am scheduling the script as a task with silent admin privileges on windows start, which may be useful if you can be bothered trying to replicate the issue.

edit: forgot to mention, if I have UAC turned on, the ahk tray icon doesn't appear at startup. The process is running but no tray icon, some kind of bug. Putting in a Reload command in the auto-execute section solved that for me (luckily reload won't trigger the UAC prompt, nor will it re-send script parameters so you can still control what happens in the script specifically at windows startup) . Another way is to add a small delay to the startup (the task scheduler only allows 30 second minimum delay, which is too long!). I think there is a special place in hell for the person(s) who created the Task Scheduler given how badly it hobbles startup items

<LogonTrigger><Enabled>true</Enabled><Delay>PT3S</Delay>;3 second delay, try longer if the tray icon refuses to appear with UAC enabled</LogonTrigger>

edit2: seems like the <Priority>2</Priority> is solving the trayicon-not-appearing problem as well.edit3: now the tray icon is showing even with the default <Priority>7</Priority>. The only thing I can think of is I ran CCleaner in the last 24 hours, maybe that cleaned out whatever was causing the tray icon bug.

Using <Priority>4</Priority> in the XMLs for the two AutoHotkey scheduled tasks I have here on my Windows 10 system has always worked fine for me (they start with normal process priority, normal I/O priority and normal page priority). Still, if you want to have a go at setting them yourself for the current process, then call SetPriorities():

qwerty12 wrote:Using <Priority>4</Priority> in the XMLs for the two AutoHotkey scheduled tasks I have here on my Windows 10 system has always worked fine for me (they start with normal process priority, normal I/O priority and normal page priority). Still, if you want to have a go at setting them yourself for the current process, then call SetPriorities():

Remember the default for normal tasks is "normal,8,5" so the only way I can get normal page priority is by using <Priority>2</Priority> in XML, and then reducing CPU priority back down to 8 with Process, Priority,, N (or reload which also does the same thing).

Sorry to be a pain but are you able to confirm that <Priority>4</Priority> on your machine does in fact result in page priority of 5 and not 4?

#NoEnv; Recommended for performance and compatibility with future AutoHotkey releases.; #Warn ; Enable warnings to assist with detecting common errors.SendMode Input ; Recommended for new scripts due to its superior speed and reliability.SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.#Persistent

Thanks. Looks like Windows 10 sets the priorities right. Must be a bug in Windows 7 or something specific to my machine causing it. The only thing I notice is the "task version" tag is 1.4 vs 1.2, maybe they changed it in between.