We help IT Professionals succeed at work.

how to change priority of main program

robert_marquardt
on
Medium Priority
2,979 Views
Last Modified: 2008-04-25
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.
Comment
Watch Question

Commented:
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)

Commented:
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.

Author

Commented:
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.

Commented:
Hmmm... How does your main thread main loop look like? Are you waiting for messages? Or doing an Application.ProcessMessages or what?

Author

Commented:
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.

Commented:
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.

Author

Commented:
Then all should be OK. I use Application.HookMainWindow which is effectively just another OnMessage handler.
Commented:
And then you're using Application.Run (which internally uses Application.HandleMessage)? Then your main thread should only consume CPU resources, if messages are in the queue. So I think, you don't need to change the priorities. To be sure, you should check your main thread's CPU usage with a propriate tool (e.g. WinTop for win9x).

Regards, Madshi.