Run/RunWait and set process priority Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 5760
Joined: 19 Dec 2016, 01:58
Location: UK

Run/RunWait and set process priority

26 Apr 2017, 17:03

Is this the best way to achieve 'RunWait low priority' without using SetTimer?

Basically I had some downloads going and had a blue screen error, and would rather that didn't happen again!

Perhaps reducing the process priority will prevent the system getting overloaded.

Code: Select all

;using:
;commands as functions (AHK v2 functions for AHK v1) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=37&t=29689

Run, % vTarget,, Hide UseErrorLevel, vPID
ProcessSetPriority("Low", vPID)
Loop
{
	if !ProcessExist(vPID)
		break
	Sleep 5000
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Run/RunWait and set process priority

27 Apr 2017, 19:17

Process has WaitClose which I guess essentially does the same thing as your loop but checks every 100ms instead. But so does RunWait. You can do it without polling but I don't want to peddle shit advice in case I'm being shortsighted; I assume polling in AutoHotkey is more reliable than what I'm thinking of.

P.S. If your computer is BSODing while downloading (esp. with torrents), it's a good indicator you need to update your NIC/Wi-Fi adapter drivers. Adjusting process priority probably won't help.
User avatar
jeeswg
Posts: 5760
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Run/RunWait and set process priority

27 Apr 2017, 19:30

Process and WaitClose, great call, cheers.

Thanks for the additional information. I think the downloading plus the PC stalling on opening one or two Internet Explorer tabs, is what caused the problem. I saw it stall for a few seconds spookily, it wasn't an instant BSOD. I hadn't had a BSOD for ages, this year?

I always have 17 AHK scripts, possibly plus a few, running, so I don't know that if the PC is stalling, that can ever be 'the straw that broke the camel's back'.

I had been curious, out of interest, in case you could run a program, specifying a specific priority, before it even loaded.

[EDIT:] I don't do that much with hardware, so obvious things may be missed by me.
Last edited by jeeswg on 27 Apr 2017, 19:53, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Run/RunWait and set process priority  Topic is solved

27 Apr 2017, 19:51

jeeswg wrote:Thanks for the additional information. I think the downloading plus the PC stalling on opening one or two Internet Explorer tabs, is what caused the problem. I saw it stall for a few seconds spookily, it wasn't an instant BSOD. I hadn't had a BSOD for ages, this year?
Just earlier with five Chrome windows open (~30 tabs in each) and Spotify running, right-clicking an AutoHotkey file caused my laptop to BSOD :/ Since in Windows 8 and 10, they dumbed down the BSOD screen and I have no idea how to use WinDbg, I have no idea why. Though in my case, my laptop's pretty old and it doesn't really get driver updates anymore...
I always have 17 AHK scripts, possibly plus a few, running, so I don't know that if the PC is stalling, that can ever be 'the straw that broke the camel's back'.
That's quite a bit :o I know it depends on what you're doing with it, but AutoHotkey itself seems very lightweight to me.
I had been curious, out of interest, in case you could run a program, specifying a specific priority, before it even loaded.
CreateProcess lets you do it (see the documentation for dwCreationFlags), but depending on what vTarget is, CreateProcess might not work to start it - and ShellExecuteEx doesn't seem to have any way of setting the priority beforehand.
User avatar
jeeswg
Posts: 5760
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Run/RunWait and set process priority

27 Apr 2017, 22:38

There, got it working.

Code: Select all

;ABOVE_NORMAL_PRIORITY_CLASS := 0x8000
;BELOW_NORMAL_PRIORITY_CLASS := 0x4000
;REALTIME_PRIORITY_CLASS := 0x100
;HIGH_PRIORITY_CLASS := 0x80
;IDLE_PRIORITY_CLASS := 0x40 ;low priority class
;NORMAL_PRIORITY_CLASS := 0x20

q:: ;Run command - create hidden (+ low priority), then show
DetectHiddenWindows, On
;IDLE_PRIORITY_CLASS := 0x40 ;low priority class
vCreationFlags := 0x40
;STARTF_USESHOWWINDOW := 0x1 ;SW_HIDE := 0
VarSetCapacity(STARTUPINFO, A_PtrSize=8?104:68, 0)
NumPut(0x1, STARTUPINFO, A_PtrSize=8?60:44, "UInt") ;dwFlags
VarSetCapacity(PROCESS_INFORMATION, A_PtrSize=8?24:16, 0)

vTarget = "C:\Windows\System32\notepad.exe" "%A_ScriptFullPath%"
DllCall("CreateProcess", Ptr,0, Str,vTarget, Ptr,0, Ptr,0, Int,1, UInt,vCreationFlags, Ptr,0, Str,A_ScriptDir, Ptr,&STARTUPINFO, Ptr,&PROCESS_INFORMATION)
vPID := NumGet(PROCESS_INFORMATION, A_PtrSize=8?16:8, "UInt") ;dwProcessId
Sleep 3000
WinShow, % "ahk_class Notepad ahk_pid " vPID
WinActivate, % "ahk_class Notepad ahk_pid " vPID
return

w:: ;Run command - create hidden, then show
DetectHiddenWindows, On
vTarget = "C:\Windows\System32\notepad.exe" "%A_ScriptFullPath%"
Run, % vTarget,, Hide UseErrorLevel, vPID
WinWait, % "ahk_class Notepad ahk_pid " vPID
Sleep 3000
WinShow, % "ahk_class Notepad ahk_pid " vPID
WinActivate, % "ahk_class Notepad ahk_pid " vPID
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Xtra and 87 guests