Posts: 175
Threads: 43
Joined: Jun 2004
How can set/get the priority of a running program in the OS (ie: Normal, AboveNormal, Real Time). I found a command on MSDN but could not get it to work.
Matt B
Matt B
Posts: 12,095
Threads: 142
Joined: Dec 2002
Function SetProcessPriority
;/
function hwnd priority [flags]
;Sets priority class of a process (running program).
;On failure generates error.
;hwnd - handle of some window of that process.
;flags - 1 hwnd is process id.
;priority - one of x_PRIORITY_x constants:
;;;def NORMAL_PRIORITY_CLASS 0x00000020 ;;normal, default
;;;def IDLE_PRIORITY_CLASS 0x00000040 ;;lowest
;;;def HIGH_PRIORITY_CLASS 0x00000080
;;;def REALTIME_PRIORITY_CLASS 0x00000100 ;;highest, dangerous
;;;def BELOW_NORMAL_PRIORITY_CLASS 0x00004000 ;;*
;;;def ABOVE_NORMAL_PRIORITY_CLASS 0x00008000 ;;*
;;;;* not supported on NT/98/Me
;EXAMPLES
;int hwnd=win("Notepad")
;SetProcessPriority hwnd IDLE_PRIORITY_CLASS
;;Eat all CPU time. Press Pause to stop.
;rep() int i=0
;;Try to work now in notepad...
;
;int pid=ProcessNameToId("notepad")
;if(!pid) end "the process is not running"
;SetProcessPriority pid BELOW_NORMAL_PRIORITY_CLASS 1
def PROCESS_SET_INFORMATION 0x0200
dll kernel32 #SetPriorityClass hProcess dwPriorityClass
int pid ph ok
if(flags&1) pid=hwnd; else GetWindowThreadProcessId(hwnd &pid)
ph=OpenProcess(PROCESS_SET_INFORMATION 0 pid); if(!ph) goto g1
ok=SetPriorityClass(ph priority)
;g1
if(!ok) _s.dllerror
if(ph) CloseHandle ph
if(!ok) end _s
December 27 2007
Fixed bug: flag 1 does not work.
Posts: 1,271
Threads: 399
Joined: Mar 2003
what about the opposite ?
GetProcessPriority
i would invoke trigger win+p to first get the actual priority
and then show a menu at mouse with other SetProcessPriority
options.
Posts: 12,095
Threads: 142
Joined: Dec 2002
Function GetProcessPriority
;/
function# hwnd [flags]
;Gets priority class of a process (running program).
;On failure generates error.
;Returns one of x_PRIORITY_x constants:
;;;def NORMAL_PRIORITY_CLASS 0x00000020 ;;normal, default
;;;def IDLE_PRIORITY_CLASS 0x00000040 ;;lowest
;;;def HIGH_PRIORITY_CLASS 0x00000080
;;;def REALTIME_PRIORITY_CLASS 0x00000100 ;;highest, dangerous
;;;def BELOW_NORMAL_PRIORITY_CLASS 0x00004000 ;;*
;;;def ABOVE_NORMAL_PRIORITY_CLASS 0x00008000 ;;*
;;;;* not supported on NT/98/Me
;hwnd - handle of some window of that process.
;flags - 1 hwnd is process id.
;EXAMPLE
;out "0x%X" GetProcessPriority(win("Notepad"))
int pid ph pr
if(flags&1) pid=hwnd; else GetWindowThreadProcessId(hwnd &pid)
ph=OpenProcess(PROCESS_QUERY_X_INFORMATION 0 pid); if(!ph) goto g1
pr=GetPriorityClass(ph)
;g1
if(!pr) _s.dllerror
if(ph) CloseHandle ph
if(!pr) end _s
ret pr
Posts: 473
Threads: 33
Joined: Aug 2007
I have Windows Vista, is there a Function for SetAffinity?
Taking on Quick Macros one day at a time
Posts: 12,095
Threads: 142
Joined: Dec 2002
Would be similar to SetProcessPriority, but instead of SetPriorityClass use SetProcessAffinityMask.
Posts: 473
Threads: 33
Joined: Aug 2007
It doesn't seem to work for me.
Function
SetProcessPriority2
;/
function hwnd priority [flags]
;Sets priority class of a process (running program).
;On failure generates error.
;hwnd - handle of some window of that process.
;flags - 1 hwnd is process id.
;priority - one of x_PRIORITY_x constants:
;;;def NORMAL_PRIORITY_CLASS 0x00000020 ;;normal, default
;;;def IDLE_PRIORITY_CLASS 0x00000040 ;;lowest
;;;def HIGH_PRIORITY_CLASS 0x00000080
;;;def REALTIME_PRIORITY_CLASS 0x00000100 ;;highest, dangerous
;;;def BELOW_NORMAL_PRIORITY_CLASS 0x00004000 ;;*
;;;def ABOVE_NORMAL_PRIORITY_CLASS 0x00008000 ;;*
;;;;* not supported on NT/98/Me
;EXAMPLES
;int hwnd=win("Notepad")
;SetProcessPriority hwnd IDLE_PRIORITY_CLASS
;;Eat all CPU time. Press Pause to stop.
;rep() int i=0
;;Try to work now in notepad...
;int pid=ProcessNameToId("notepad")
;if(!pid) end "the process is not running"
;SetProcessPriority pid BELOW_NORMAL_PRIORITY_CLASS 1
def PROCESS_SET_INFORMATION 0x0200
dll kernel32 #SetProcessAffinityMask hProcess dwPriorityClass
int pid ph ok
if(flags&1) pid=hwnd; else GetWindowThreadProcessId(hwnd &pid)
ph=OpenProcess(PROCESS_SET_INFORMATION 0 pid); if(!ph) goto g1
ok=SetProcessAffinityMask(ph priority)
;g1
if(!ok) _s.dllerror
if(ph) CloseHandle ph
if(!ok) end _s
I need to change the code for this so I can uncheck CPU 0 in the Set Affinity option but I couldn't find it in the msdn library.
;;;def NORMAL_PRIORITY_CLASS 0x00000020 ;;normal, default
;;;def IDLE_PRIORITY_CLASS 0x00000040 ;;lowest
;;;def HIGH_PRIORITY_CLASS 0x00000080
;;;def REALTIME_PRIORITY_CLASS 0x00000100 ;;highest, dangerous
;;;def BELOW_NORMAL_PRIORITY_CLASS 0x00004000 ;;*
;;;def ABOVE_NORMAL_PRIORITY_CLASS 0x00008000 ;;*
Taking on Quick Macros one day at a time
Posts: 12,095
Threads: 142
Joined: Dec 2002
How do you call it. And how know that is does not work?
Posts: 473
Threads: 33
Joined: Aug 2007
I tried this
Macro
int hwnd=win("Notepad")
SetProcessPriority2 hwnd IDLE_PRIORITY_CLASS
and I figured that if I change the "DILE_PRIORITY_CLASS" it might work.
Taking on Quick Macros one day at a time
Posts: 12,095
Threads: 142
Joined: Dec 2002
Argument #2 must be affinity mask, not priority class. Read SetProcessAffinityMask help. To turn off the first cpu in that process, the mask will be 2 (assuming there are 2 CPU).
Posts: 12,095
Threads: 142
Joined: Dec 2002
Quote:uncheck CPU 0 in the Set Affinity option
Where is the option? If it is an option in Control Panel, this function does not uncheck it. Don't know how.
Posts: 473
Threads: 33
Joined: Aug 2007
Task Manager --> Processes Tab --> Right click on Internet Explorer --> Set Affinity --> Then I get this, and I want to uncheck CPU 0, everytime internet explorer is opened.
Taking on Quick Macros one day at a time
Posts: 12,095
Threads: 142
Joined: Dec 2002
Function
SetProcessAffinity
;/
function hwnd affinityMask [flags] ;;flags: 1 hwnd is process id
;Sets affinity of a process (running program). It is which processors the process can use.
;On failure generates error.
;hwnd - handle of some window of that process.
;affinityMask - sum of flags, where flag 1 is for CPU0, flag 2 is for CPU1, flag 4 is for CPU2, flag 8 is for CPU3, .... For example, affinityMask 2 allows to use only CPU1.
;EXAMPLE
;SetProcessAffinity ProcessNameToId("notepad") 1 1 ;;let notepad use only CPU0
int pid ph ok
if(flags&1) pid=hwnd; else GetWindowThreadProcessId(hwnd &pid)
__Handle hp=OpenProcess(PROCESS_SET_INFORMATION 0 pid); if(!hp) goto g1
ok=SetProcessAffinityMask(hp affinityMask)
;g1
if(!ok) end _s.dllerror
Posts: 473
Threads: 33
Joined: Aug 2007
Macro
Trigger
!ca"Internet Explorer provided by Dell" "IEFrame"
SetProcessAffinity ProcessNameToId("iexplore") 2 1
Works great, thanks alot.
Taking on Quick Macros one day at a time
Posts: 2
Threads: 0
Joined: Dec 2016
Too confusing. Please any other working, simple example how i can set priority for my macros on Win 7.
Posts: 2
Threads: 0
Joined: Dec 2016
Answer to myself question, may help someone else. Just simply add the following line to your macros.
SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS)