ChangePriority API does not seem to work

CharlesSHill
CharlesSHill used Ask the Experts™
on
Code below uses the API to change the application's priority. It does not seem to work.
If I go to TaskManager I can set the priority by hand but the code below does not seem to work.
Also, Win2K lists additional priorities such as AboveNormal. What are their constants.



Option Explicit

Public Enum PRIORITY_CLASS
  NORMAL_PRIORITY_CLASS = &H20
  IDLE_PRIORITY_CLASS = &H40
  HIGH_PRIORITY_CLASS = &H80
  REALTIME_PRIORITY_CLASS = &H100
End Enum

Private Const PROCESS_DUP_HANDLE = &H40

Private Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, _
   ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long
   
Private Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long
 
Private Declare Function GetCurrentProcessId _
  Lib "kernel32" () As Long
 
Private Declare Function SetPriorityClass Lib "kernel32" _
 (ByVal hProcess As Long, ByVal dwPriorityClass As Long) _
  As Long

Public Function ChangePriority(dwPriorityClass As _
   PRIORITY_CLASS) As Boolean
  Dim hProcess As Long
  Dim ret As Long, pid As Long
  pid = GetCurrentProcessId()
  hProcess = OpenProcess(PROCESS_DUP_HANDLE, True, pid)
  If hProcess = 0 Then Exit Function
  ret = SetPriorityClass(hProcess, dwPriorityClass)
  Call CloseHandle(hProcess)
  ChangePriority = ret <> 0
End Function

Private Sub Command1_Click()
   Call ChangePriority(HIGH_PRIORITY_CLASS)
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this

Const THREAD_BASE_PRIORITY_IDLE = -15
Const THREAD_BASE_PRIORITY_LOWRT = 15
Const THREAD_BASE_PRIORITY_MIN = -2
Const THREAD_BASE_PRIORITY_MAX = 2
Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MIN
Const THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAX
Const THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)
Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)
Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLE
Const THREAD_PRIORITY_NORMAL = 0
Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRT
Const HIGH_PRIORITY_CLASS = &H80
Const IDLE_PRIORITY_CLASS = &H40
Const NORMAL_PRIORITY_CLASS = &H20
Const REALTIME_PRIORITY_CLASS = &H100
Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long
Private Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim hThread As Long, hProcess As Long
    'retrieve the current thread and process
    hThread = GetCurrentThread
    hProcess = GetCurrentProcess
    'set the new thread priority to "lowest"
    SetThreadPriority hThread, THREAD_PRIORITY_LOWEST
    'set the new priority class to "idle"
    SetPriorityClass hProcess, IDLE_PRIORITY_CLASS
    'print some results
    Me.AutoRedraw = True
    Me.Print "Current Thread Priority:" + Str$(GetThreadPriority(hThread))
    Me.Print "Current Priority Class:" + Str$(GetPriorityClass(hProcess))
End Sub

Author

Commented:
Thanks a lot - it worked fine!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial