Jump to content
  • Advertisement

Archived

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

VanKurt

Using LESS system resources

This topic is 5380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
it is simply eating all cpu becouse it dont give time to windows to perform his multitasking stuff

either make it do something more complex, or add some sleep() calls to solve that

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!