Using LESS system resources

Started by
17 comments, last by VanKurt 20 years, 5 months ago
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!!!!
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
"Everything works out in the end, if it doesn't then it is not the end"
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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?
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();		}	   	}
EasyGL - easy to use graphics library.
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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
"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.
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.

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.

This topic is closed to new replies.

Advertisement