C++Introduction to C++Language FeatureLearn C++Syntax

How To Set Runtime Process Priority On Windows In A C++ App

The C++ Builder VCL library has a lot of Windows-specific libraries for controlling the runtime behavior of our applications. We can easily change the process priority of the current task in thread/core. In this post, you’ll get answers to these questions:

  • How I set runtime process priority on my Windows C++ application?
  • Can I change the process priority level at runtime?
  • May I apply specific process priorities to different actions?
  • How can use full power of one of my CPU core or thread?
  • How can I use maximum power of my CPU in an efficient way?

By learning how To Set Runtime Process Priority On Windows In A C++ App, it will help you to build C++ applications with the use of a C++ IDE.

What is the SetPriorityClass method in C++?

SetPriorityClass method sets the priority class for the specified process and included in processthreadsapi.h library. A handle to the process and the priority class for the process should be given as a parameter. This class value together with the priority value of each thread of the process determines each thread’s base priority level.

What is the syntax of the SetPriorityClass method in C++?

Here is the syntax of the SetPriorityClass method.

Syntax:

Here,

  • the handle_process parameter is the handle to process, we can directly use GetCurrentProcess() method as a parameter to set the priority of the current process
  • and priority_param is the priority class parameter for the given process.

What are the parameters which can be used with the SetPriorityClass method ?

In this SetPriorityClass() method we can use; REALTIME_PRIORITY_CLASS , NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS, IDLE_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS , PROCESS_MODE_BACKGROUND_BEGIN, PROCESS_MODE_BACKGROUND_END parameters.

ABOVE_NORMAL_PRIORITY_CLASS
0x00008000
Process that has priority above NORMAL_PRIORITY_CLASS but below HIGH_PRIORITY_CLASS.
BELOW_NORMAL_PRIORITY_CLASS
0x00004000
Process that has priority above IDLE_PRIORITY_CLASS but below NORMAL_PRIORITY_CLASS.
HIGH_PRIORITY_CLASS
0x00000080
Process that performs time-critical tasks that must be executed immediately. The threads of the process preempt the threads of normal or idle priority class processes. An example is the Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class application can use nearly all available CPU time.
IDLE_PRIORITY_CLASS
0x00000040
Process whose threads run only when the system is idle. The threads of the process are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle-priority class is inherited by child processes.
NORMAL_PRIORITY_CLASS
0x00000020
Process with no special scheduling needs.
PROCESS_MODE_BACKGROUND_BEGIN
0x00100000
Begin background processing mode. The system lowers the resource scheduling priorities of the process (and its threads) so that it can perform background work without significantly affecting activity in the foreground.This value can be specified only if hProcess is a handle to the current process. The function fails if the process is already in background processing mode.Windows Server 2003 and Windows XP:  This value is not supported.
PROCESS_MODE_BACKGROUND_END
0x00200000
End background processing mode. The system restores the resource scheduling priorities of the process (and its threads) as they were before the process entered background processing mode.This value can be specified only if hProcess is a handle to the current process. The function fails if the process is not in background processing mode.Windows Server 2003 and Windows XP:  This value is not supported.
REALTIME_PRIORITY_CLASS
0x00000100
Process that has the highest possible priority. The threads of the process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.
Resource: ( https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass )

Is there a simple example of how to use the SetPriorityClass method in C++?

Here is the simple example for the SetPriorityClass Method,

Is there a longer example of using the SetPriorityClass method in C++?

Here is the full example that shows how to use SetPriorityClass Method before and after heavy calculations,

Please be careful when you use REALTIME_PRIORITYCLASS! Using this flag the SetPriorityClass method tells Windows that a thread should use the highest possibly priority. That means if you have a lot of cores/threads you might completely monopolize the CPU core. In multi-tasking applications this may result full use of all available CPU cores resulting in the computer and Windows operating system becoming unresponsive due to there being no available time left for the CPU to do anything else. This may also cause high temperatures on the CPU and even burn out the CPU or motherboard if the cooling system is not designed for prolonged maximum CPU usage.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?