Link to home
Start Free TrialLog in
Avatar of Informative
InformativeFlag for United States of America

asked on

Code to get a VB executable alter its own priority to Above Normal or High

rather than running at normal which is the default for all apps I would like the app to be able to alter its own priority to run faster as might be useful under user control..
Avatar of Informative
Informative
Flag of United States of America image

ASKER

Oh the OS should work ideally under several OS environments ME/NT/XP/2000/2003 but if I had to choose one or two XP and 2003 would be best.
Avatar of Mikal613
If you start the App from the Command Line then all you do is

/High ProgramPath
Const THREAD_BASE_PRIORITY_IDLE = -15Const THREAD_BASE_PRIORITY_LOWRT = 15Const THREAD_BASE_PRIORITY_MIN = -2Const THREAD_BASE_PRIORITY_MAX = 2Const THREAD_PRIORITY_LOWEST = THREAD_BASE_PRIORITY_MINConst THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAXConst THREAD_PRIORITY_BELOW_NORMAL = (THREAD_PRIORITY_LOWEST + 1)Const THREAD_PRIORITY_ABOVE_NORMAL = (THREAD_PRIORITY_HIGHEST - 1)Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLEConst THREAD_PRIORITY_NORMAL = 0Const THREAD_PRIORITY_TIME_CRITICAL = THREAD_BASE_PRIORITY_LOWRTConst HIGH_PRIORITY_CLASS = &H80Const IDLE_PRIORITY_CLASS = &H40Const NORMAL_PRIORITY_CLASS = &H20Const REALTIME_PRIORITY_CLASS = &H100Private Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As LongPrivate Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As LongPrivate Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As LongPrivate Declare Function GetPriorityClass Lib "kernel32" (ByVal hProcess As Long) As LongPrivate Declare Function GetCurrentThread Lib "kernel32" () As LongPrivate Declare Function GetCurrentProcess Lib "kernel32" () As LongPrivate 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
ASKER CERTIFIED SOLUTION
Avatar of Mikal613
Mikal613
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dang123
Dang123

Listening
Private Sub Form_Load()  
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_HIGHEST 'set the new priority class to "idle"  
 SetPriorityClass hProcess,HIGH_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
That does indeed work as advertised - tested under XP so far.  great job thanks!