by Jack van Niekerk (2 Submissions)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 25th January 2001
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
Set priority of a thread
API Declarations
Declare Function SetThreadPriority Lib "kernel32" (ByVal hThread As Long, ByVal nPriority As Long) As Long
Declare Function GetThreadPriority Lib "kernel32" (ByVal hThread As Long) As Long
Declare Function GetCurrentThread Lib "kernel32" () As Long
Public Enum lgThreadPrioritySetting
lgNormal = 0
lgAboveNormal = 1
lgHighest = 2
lgTimeCritical = 15
lgBelowNormal = -1
lgLowest = -2
lgIdle = -15
End Enum
Dim vRep As Long
Dim vThrd As Long
Dim vNewP As Long
'evaluate new thread priority setting
If IsMissing(vNewPriority) Then
'default to 0
vNewP = 0
Else
Select Case vNewPriority
Case 0, 1, 2, 15, -1, -2, -15
'all ok
vNewP = vNewPriority
Case Else
'default to 0
vNewP = 0
End Select
End If
'get the current app's thread - you can also set the priority of another app if you obtain it's thread (excludes system threads)
vThrd = GetCurrentThread
'set the thread's priority
vRep = SetThreadPriority(vThrd, 2)
If vRep = 0 Then
MsgBox "The requested priority setting of " & vNewPriority & " failed.", vbOKOnly + vbCritical, "Priority Setting Failed"
End If
'priorities:
' 0 normal
' 1 above normal
' 2 highest
' 15 time critical (not a good option because it will
' halt almost everything else. That includes
' the repainting of other app's forms when this
' app closes a form that was on top of the other.
' But hey, if you need the attention then
' go for it)
' -1 below normal
' -2 lowest
' -15 idle (very nice setting if you want to run a
' non critical background app that should only
' run when the processor is idle. - a very
' stealthy app.)
'General - It may not be a good idea to set the priority up and down
' during the running of an app. I could find no direct
' documentation on what it will do but the way in which threads
' are dealt with in the processor and some common logic tells
' me that some computers may not like it.
End Sub