Process Priority Class
The process priority class describes the priority of a particular process running on the system. Win32 supports four distinct priority classes:
- Idle
- Normal
- High
- Realtime
The default priority class for any process, of course, is Normal. Each of these priority classes has a corresponding flag defined in the Windows unit. You can or any of these flags with the dwCreationFlags parameter of CreateProcess() in order to spawn a process with a specific priority. Additionally, you can use these flags to dynamically adjust the priority class of a given process, as shown in a moment. Furthermore, each priority class can also be represented by a numeric priority level, which is a value between 4 and 24 (inclusive).
NOTE
Modifying a process's priority class requires special process privileges under Windows NT/2000. The default settings allow processes to set their priority classes, but these can be turned off by system administrators, particularly on high-load Windows NT/2000 servers.
Table 1 shows each priority class and its corresponding flag and numeric value.
Table 1 Process Priority Classes
Class |
Flag |
Value |
Idle |
IDLE_PRIORITY_CLASS |
$40 |
Below Normal* |
BELOW_NORMAL_PRIORITY_CLASS |
$4000 |
Normal |
NORMAL_PRIORITY_CLASS |
$20 |
Above Normal* |
ABOVE_NORMAL_PRIORITY_CLASS |
$8000 |
High |
HIGH_PRIORITY_CLASS |
$80 |
Realtime |
REALTIME_PRIORITY_CLASS |
$100 |
To get and set the priority class of a given process dynamically, Win32 provides the GetPriorityClass() and SetPriorityClass() functions, respectively. These functions are defined as follows:
function GetPriorityClass(hProcess: THandle): DWORD; stdcall; function SetPriorityClass(hProcess: THandle; dwPriorityClass: DWORD): BOOL; stdcall;
The hProcess parameter in both cases represents a handle to a process. In most cases, you'll be calling these functions in order to access the priority class of your own process. In that case, you can use the GetCurrentProcess() API function. This function is defined as follows:
function GetCurrentProcess: THandle; stdcall;
The return value of these functions is a pseudo-handle for the current process. We say pseudo because the function doesn't create a new handle, and the return value doesn't have to be closed with CloseHandle(). It merely provides a handle that can be used to reference an existing handle.
To set the priority class of your application to High, use code similar to the following:
if not SetPriorityClass(GetCurrentProcess, HIGH_PRIORITY_CLASS) then ShowMessage('Error setting priority class.');
CAUTION
In almost all cases, you should avoid setting the priority class of any process to Realtime. Because most of the operating system threads run in a priority class lower than Realtime, your thread will receive more CPU time than the OS itself, and that could cause some unexpected problems.
Even bumping the priority class of the process to High can cause problems if the threads of the process don't spend most of their time idle or waiting for external events (such as file I/O). One high-priority thread is likely to drain all CPU time away from lower-priority threads and processes until it blocks on an event, goes idle, or processes messages. Preemptive multitasking can easily be defeated by abusing scheduler priorities.