SetPriorityClass function (processthreadsapi.h)
Sets the priority class for the specified process. This value together with the priority value of each thread of the process determines each thread's base priority level.
Syntax
BOOL SetPriorityClass(
[in] HANDLE hProcess,
[in] DWORD dwPriorityClass
);
Parameters
[in] hProcess
A handle to the process.
The handle must have the PROCESS_SET_INFORMATION access right. For more information, see Process Security and Access Rights.
[in] dwPriorityClass
The priority class for the process. This parameter can be one of the following values.
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Every thread has a base priority level determined by the thread's priority value and the priority class of its process. The system uses the base priority level of all executable threads to determine which thread gets the next slice of CPU time. The SetThreadPriority function enables setting the base priority level of a thread relative to the priority class of its process. For more information, see Scheduling Priorities.
The *_PRIORITY_CLASS values affect the CPU scheduling priority of the process. For processes that perform background work such as file I/O, network I/O, or data processing, it is not sufficient to adjust the CPU scheduling priority; even an idle CPU priority process can easily interfere with system responsiveness when it uses the disk and memory. Processes that perform background work should use the PROCESS_MODE_BACKGROUND_BEGIN and PROCESS_MODE_BACKGROUND_END values to adjust their resource scheduling priorities; processes that interact with the user should not use PROCESS_MODE_BACKGROUND_BEGIN.
If a process is in background processing mode, the new threads it creates will also be in background processing mode. When a thread is in background processing mode, it should minimize sharing resources such as critical sections, heaps, and handles with other threads in the process, otherwise priority inversions can occur. If there are threads executing at high priority, a thread in background processing mode may not be scheduled promptly, but it will never be starved.
Each thread can enter background processing mode independently using SetThreadPriority. Do not call SetPriorityClass to enter background processing mode after a thread in the process has called SetThreadPriority to enter background processing mode. After a process ends background processing mode, it resets all threads in the process; however, it is not possible for the process to know which threads were already in background processing mode.
Examples
The following example demonstrates the use of process background mode.
#include <windows.h>
#include <tchar.h>
int main( void )
{
DWORD dwError, dwPriClass;
if(!SetPriorityClass(GetCurrentProcess(), PROCESS_MODE_BACKGROUND_BEGIN))
{
dwError = GetLastError();
if( ERROR_PROCESS_MODE_ALREADY_BACKGROUND == dwError)
_tprintf(TEXT("Already in background mode\n"));
else _tprintf(TEXT("Failed to enter background mode (%d)\n"), dwError);
goto Cleanup;
}
// Display priority class
dwPriClass = GetPriorityClass(GetCurrentProcess());
_tprintf(TEXT("Current priority class is 0x%x\n"), dwPriClass);
//
// Perform background work
//
;
if(!SetPriorityClass(GetCurrentProcess(), PROCESS_MODE_BACKGROUND_END))
{
_tprintf(TEXT("Failed to end background mode (%d)\n"), GetLastError());
}
Cleanup:
// Clean up
;
return 0;
}
Requirements
Requirement | Value |
---|---|
Minimum supported client | Windows XP [desktop apps | UWP apps] |
Minimum supported server | Windows Server 2003 [desktop apps | UWP apps] |
Target Platform | Windows |
Header | processthreadsapi.h (include Windows.h on Windows Server 2003, Windows Vista, Windows 7, Windows Server 2008 Windows Server 2008 R2) |
Library | Kernel32.lib |
DLL | Kernel32.dll |