Informative
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..
If you start the App from the Command Line then all you do is
/High ProgramPath
/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_MINCo nst THREAD_PRIORITY_HIGHEST = THREAD_BASE_PRIORITY_MAXCo nst THREAD_PRIORITY_BELOW_NORM AL = (THREAD_PRIORITY_LOWEST + 1)Const THREAD_PRIORITY_ABOVE_NORM AL = (THREAD_PRIORITY_HIGHEST - 1)Const THREAD_PRIORITY_IDLE = THREAD_BASE_PRIORITY_IDLEC onst THREAD_PRIORITY_NORMAL = 0Const THREAD_PRIORITY_TIME_CRITI CAL = THREAD_BASE_PRIORITY_LOWRT Const 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(hTh read)) Me.Print "Current Priority Class:" + Str$(GetPriorityClass(hPro cess))End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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_HI GHEST 'set the new priority class to "idle"
SetPriorityClass hProcess,HIGH_PRIORITY_CLA SS 'print some results
Me.AutoRedraw = True
Me.Print "Current Thread Priority:" + Str$(GetThreadPriority(hTh read))
Me.Print "Current Priority Class:" + Str$(GetPriorityClass(hPro cess))
End Sub
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_HI
SetPriorityClass hProcess,HIGH_PRIORITY_CLA
Me.AutoRedraw = True
Me.Print "Current Thread Priority:" + Str$(GetThreadPriority(hTh
Me.Print "Current Priority Class:" + Str$(GetPriorityClass(hPro
End Sub
ASKER
That does indeed work as advertised - tested under XP so far. great job thanks!
ASKER