Important alert: (current site time 7/16/2013 4:10:49 AM EDT)
 

VB icon

Changing priority

Email
Submitted on: 4/28/1999
By: Steve Pepper 
Level: Not Given
User Rating: By 5 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 29998
 
     This code fragment demonstrates how you can get a program to change it's own priority. Sometimes it is necessary to change the priority of a process from the default. The example that prompted this code was a program I had to launch from a commercial scheduler always defaulted to idle priority. This caused the program to miss it's processing deadline. Increasing the priority to normal or high solved the problem.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Changing priority
'**************************************
Option Explicit
Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const IDLE_PRIORITY_CLASS = &H40
Private Const HIGH_PRIORITY_CLASS = &H80
Private Const REALTIME_PRIORITY_CLASS = &H100
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)
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Changing priority
' Description:This code fragment demonstrates how you can get a program to change it's own priority.
Sometimes it is necessary to change the priority of a process from the default.
The example that prompted this code was a program I had to launch from a commercial scheduler always defaulted to idle priority. This caused the program to miss it's processing deadline. Increasing the priority to normal or high solved the problem.
' By: Steve Pepper
'
' Assumes:To run this code, add a form to a new project with a timer and a label.
The priority will toggle between idle and high every two seconds.
Under NT4, you can use the task manager to see the base priority of this process changing.
'
' Side Effects:Changing the priority of your process to REALTIME_PRIORITY_CLASS is a bad idea in Visual Basic.
I have only tested this code under NT4 sp4 and VB5 but I think it should work under Windows9x
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1683&lngWId=1'for details.'**************************************

Sub ChangePriority(dwPriorityClass As Long)
Dim hProcess&
Dim ret&, pid&
pid = GetCurrentProcessId() ' get my proccess id
' get a handle to the process
hProcess = OpenProcess(PROCESS_DUP_HANDLE, True, pid)
If hProcess = 0 Then
Err.Raise 2, "ChangePriority", "Unable to open the source process"
Exit Sub
End If
' change the priority
ret = SetPriorityClass(hProcess, dwPriorityClass)
' Close the source process handle
Call CloseHandle(hProcess)
If ret = 0 Then
Err.Raise 4, "ChangePriority", "Unable to close source handle"
Exit Sub
End If
End Sub
Private Sub Form_Load()
Timer1.Interval = 2000
Call Timer1_Timer
End Sub
Private Sub Timer1_Timer()
Static Priority&
If Priority = IDLE_PRIORITY_CLASS Then
 Priority = HIGH_PRIORITY_CLASS
 Label1.Caption = "HIGH priority"
Else
 Label1.Caption = "IDLE priority"
 Priority = IDLE_PRIORITY_CLASS
End If
Call ChangePriority(Priority)
End Sub


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/2/1999 1:45:00 PMOwen M

There is one program that can see if the priority has changed under Win 4x. It is Taskinfo98. You can download it at download.com(i think!)
(If this comment was disrespectful, please report it.)

 
5/7/1999 9:17:00 PMAndy M. Lopez

Thanks for the free source code, Thax's
I just want to learn more... :>
(If this comment was disrespectful, please report it.)

 
5/11/1999 1:16:00 PM33

1






(If this comment was disrespectful, please report it.)

 
11/2/1999 9:20:00 PMChan

do u know how to close other application like MS Access or Ms word?
(If this comment was disrespectful, please report it.)

 
11/20/1999 9:33:00 AMD

This code shows how powerful VB is and how easy it is to write powerful code. Changing the priority of a process programatically is powerful stuff and you showed how easy it can be done in VB. Thanks
(If this comment was disrespectful, please report it.)

 
2/5/2000 5:51:19 AMBunzi

Fantastic Source-Code. Very useful, especially for security-tools.
(If this comment was disrespectful, please report it.)

 
11/14/2000 5:01:08 PMDan Messenger

So wot will happen if i change the priority for ALL the processes so that they are all at the lowest priority ??
(If this comment was disrespectful, please report it.)

 
1/5/2002 3:39:30 AMEvil Monkey

sweet! 5 globes from me!
(If this comment was disrespectful, please report it.)

 
7/1/2007 7:55:38 PMvivalite

On my windows XP it gives #4 error.
However I've found a way around:

Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long

Public Function ChangePriority(dwPriorityClass As Long)
Dim ret&
ret = SetPriorityClass(GetCurrentProcess, dwPriorityClass)
End Function

Where dwPriorityClass could be &H20 to &H100
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.