Archived

This topic is now archived and is closed to further replies.

VanKurt

Using LESS system resources

Recommended Posts

Hi there! I''ve written a little app that runs invisible (without) window in the back. The intention is to have an application that does actions when special keys are pressed (e.g. F9 for "run notepad"). At the moment the app does nothing. The code looks like this: int WinMain( blablabla ) { while(1) { // Do Nothing } } But when this program is running, the WHOLE COMPUTER gets SLOW. In the task-manager it says that the app is using 99% of the CPU-power (or something like this). What do I have to do to make this app not so "hungry" ??? I mean an app doing nothing (or almost nothing) shouldn''t act like that.... Thanks!!!!

Share this post


Link to post
Share on other sites
Um, don''t spin in a while(1) loop like that.

Of course it will eat all the CPU, what do you think it will do? Look up a very basic Windows programming tutorial to learn how to handle input better.

Share this post


Link to post
Share on other sites
Input? Actually I don''t want do something with input right now. I just want the app to "run". The code for doing something will be inserted later.

Are sleep() calls the solution?

Share this post


Link to post
Share on other sites
No! Sleep is not the answer. Use WaitMessage();.
Example:

while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
WaitMessage();
}
}

Share this post


Link to post
Share on other sites
quote:
Actually I don''t want do something with input right now.

quote:
The intention is to have an application that does actions when special keys are pressed (e.g. F9 for "run notepad").

If that isn''t input, I don''t know what is.

Even so you STILL have to use a proper Windows message loop.

Share this post


Link to post
Share on other sites
Wait no!

There''s a big confusion here, windows programming is event model so you have to start from a totally different perspective.
Instead of asking when there is some input you hook a function for it that will be called by the OS, in that way windows doesn''t assign any cpu time until it has something for it.
To accomplish that you''ll have to go thru the ''hooks'' API of windows, check here on msdn for an overview of it but beware, is not a very easy path and there''s some incompatibilities between 9x/nt/2k

Share this post


Link to post
Share on other sites
"sleep(0);" would indead work.

The reason your computer appears to slows down is the app is using *all* of its timeslice. And all most all apps run on the same priority level, thus you limit the total amount of time avaiable to threads at your priority level(higher priority stuff gets to go first, lower priority stuff gets to pick at the remaining time and so on).

So if you bump you app *down* a priority level, it will still do what ever you want it todo, but the GUI will still be responcive.

Thats right, you put worker threads which you expect to take up lots of CPU time at a lower priority to allow the GUI to be responsive.

Share this post


Link to post
Share on other sites
quote:
Original post by VanKurt
int WinMain( blablabla )
{
while(1)
{
// Do Nothing
}
}

...

What do I have to do to make this app not so "hungry" ???
I mean an app doing nothing (or almost nothing) shouldn''t act like that....



Your application is infinitely looping, of course its doing something. And its doing that something a lot, over and over, as fast as it can, thus the 99% CPU.

Share this post


Link to post
Share on other sites
it runs in a continous loop at a maximum rate the CPU can afford that''s why 99% of CPU utilization is consumed. However, putting up sleep() function within the loop will let the program stop for a while in the amount of delay you set on sleep(). during this period, the CPU won''t be doing anything, or it will process other task.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I read somewhere that games like half life use up 99% of CPU and it is not necessarily a "bad thing"

Share this post


Link to post
Share on other sites
Every game uses all the cpu time they can get and that''s good, but when they don''t have focus then it''s bad, a game must use a different loop when not in focus to free the cpu when not active.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
So can''t you just stop or "pause" the main loop when you''ve lost focus? Wouldn''t that free the CPU up for the other things?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
is there a way to let your program test fi it has focus so you can do one thing " on focus" and another off focus?

Share this post


Link to post
Share on other sites
Hooks are not necessary.

Testing ''do we have focus'' is still the wrong way to approach the problem. This is really just a programing Windows 101 question here. The original poster simply needs to register hotkeys for the keys to watch and process those messages inside the application.

Share this post


Link to post
Share on other sites
Sure you could do it that way, but it''d be a million times easier to use a GetKeyAsync or whatever its called. Sure it won''t be "right," but it isn''t going to use any processor at all as long as it''s calling Sleep(). You can then use a PeekMessage inside the main loop.

Share this post


Link to post
Share on other sites