Link to home
Start Free TrialLog in
Avatar of robert_marquardt
robert_marquardt

asked on

how to change priority of main program

I want to lower the priority of my main thread because the real work is done in separate threads.
It is a tray application so most of the time the main thread does not work much.
Is it possible to lower the main thread priority down to tpIdle without impact on the other threads of my program?
Of course the main thread priority should be raised back again when the main window is shown.
Avatar of cubud
cubud
Flag of United Kingdom of Great Britain and Northern Ireland image

var
  H: THandle;
begin
  H := GetCurrentProcess();
  SetPriorityClass(hProc,REALTIME_PRIORITY_CLASS);
end;


Valid values are

HIGH_PRIORITY_CLASS:
Specify this class for a 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 Windows 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:
Specify this class for a 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:
Specify this class for a process with no special scheduling needs.

REALTIME_PRIORITY_CLASS Specify this class for a 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.

Pete
====
http://www.HowToDoThings.com (Delphi articles)
http://www.Stuckindoors.com/delphi (Open source)
Avatar of Madshi
Madshi

Hi Robert!   :-)

That depends on how deep the main thread is involved with the other threads. E.g. if the other threads often call "TThread.Synchronize", it's probably not a good idea to lower the main thread priority. Or if the main thread often enters critical sections which are also used by the other threads, then you should also better leave the main thread's priority where it is. However, if the other threads are quite independent from the main thread, then you can lower the main thread's priority without any negative side effects.
But may I ask why you want to do that at all? I mean it only has any effect, if the CPU is on 100%, anyway. Only in such a case your main thread would not run very often/long, if you lower the priority. In most other cases you wouldn't even see any difference...

Regards, Madshi.
Avatar of robert_marquardt

ASKER

I have a tray application serving USB devices.
Each device gets its own thread and these threads do not Synchronize at all. They only call keybd_event or mouse_event as reaction to reading the USB device.
The main thread checks for application switches via a global hook and provides the correct application dependant settings for the worker threads.
I want to lower the priority of the main thread in the hope that it does not hog the CPU that much.
Hmmm... How does your main thread main loop look like? Are you waiting for messages? Or doing an Application.ProcessMessages or what?
Job of the main thread is waiting for the message the global hook DLL sends on switch of applications while the main form is invisible.
On reception of this message the main thread reads in some settings from the registry and the worker thread start using these settings.
Yeah, but *how* is the main thread waiting? Do you use PeekMessage / Application.ProcessMessages or GetMessage / WaitMessage / Application.HandleMessage? I'm asking because THAT should be the first thing to look at. PeekMessage / Application.ProcessMessages pushes the CPU to 100%, GetMessage / WaitMessage / Application.HandleMessage does not. If your main thread is properly programmed and does nothing but wait for messages, it should not consume *any* CPU resources while waiting, it should only consume CPU resources when a message arrives.
Then all should be OK. I use Application.HookMainWindow which is effectively just another OnMessage handler.
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial