Well, I've been programming in C++ a lot lately, and I've noticed that my programs always take an almost unbelievable amount of processing power, no matter what I do. If it is waiting for user input, then yes, however, if it is looping or doing anything else, even while waiting for user input, it takes a massive amount of processer power.
Like, say I wanted a blank program, no visual window, nothing, just a blank process that does nothing, my first assumption was that the code would look something like this:
#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
while(1){}
return 0;
}
That should open a process, and then it should go into a constant useless while loop, however, this process will take up to 50% of my CPU capacity, and I assume that is only because it's on one of my processors, and not the other, so this program basically becomes like System Idle Process, but I'm wondering how I can make it, say, constantly check for the user to hit the F4 key, and do something when they hit it, but not take up a ton of memory due to constantly checking with a loop. How would I go about doing this?
None.
Well, the issue is that I want things to still be able to happen, without waiting for user input, like Firefox, you can do something else while it's loading things, and it keeps running, but as soon as it's done it goes to 0 processor usage, instead of constantly looping waiting for input from the user.
None.
If you're making a window program in VC++, then it is always idle when there is no user input or code -- there is no need for a loop. If you're making a console program, then system("pause"); is the way to go, if you don't want to use that, here's a link:
http://msdn.microsoft.com/en-us/magazine/cc301403.aspx .
I think what you may be looking for is the Sleep function. Try using that inside the loop. It takes one parameter, the amount of time in milliseconds.
This isn't the only way, though. There are also other things you could do. I don't know what else to suggest at this point, since I don't know what kind of code you are going for.
None.
Okay, I want a program that starts another program via system commands everytime you hit F4, which from what I know means you have to constantly monitor the keyboard input, which creates that massive processor lag, how can I monitor the keyboard constantly without using up all the processor power.
It is simply a backround process, no window, no console, nothing, just a process that monitors for F4 being hit. At best it seems like I could use Sleep to at least put a bit more of a wait time between loops so it lags less, but I don't know if it'd work well enough..
None.
Putting a Sleep in the loop would have a huge effect. The minimum time you could put in would be 1 millisecond (if it even allows that low) and even that is a lot of time for the processor. Sleep basically passes control to other programs during the time you specify. It is probably otherwise going through your loop from millions to billions of times per second.
None.
I'd use like sleep(20), that is what I do. It still works for input, too.
TinyMap2 - Latest in map compression! ( 7/09/14 - New build! )
EUD Action Enabler - Lightweight EUD/EPD support! (ChaosLauncher/MPQDraft support!)
EUDDB -
topic - Help out by adding your EUDs! Or Submit reference files in the References tab!
MapSketch - New image->map generator!
EUDTrig -
topic - Quickly and easily convert offsets to EUDs! (extended players supported)
SC2 Map Texture Mask Importer/Exporter - Edit texture placement in an image editor!
This page has been viewed [img]http://farty1billion.dyndns.org/Clicky.php?img.gif[/img] times!
Ah, you're right, it's surprising how much it helps the processor, thanks guys
None.
This is called hysteresis for those wondering. You gotta put in some form of hysteresis when you're measuring something, or else you'll end up with too much data, and burn out your IC.
"Parliamentary inquiry, Mr. Chairman - do we have to call the Gentleman a gentleman if he's not one?"
Quote from name:Zachary Taylor
This is called hysteresis for those wondering. You gotta put in some form of hysteresis when you're measuring something, or else you'll end up with too much data, and burn out your IC.
Totally unrelated topic, man.
None.
Yeah.. I was gonna say..
Anyways, I added a Sleep(1) to my lil' RPG and it reduced it from a 17-25 % CPU usage to an almost constant 0% unless allocating memory.
None.
Correct me if I'm wrong... but I was under the impression that Sleep(0) was best if your looping, and that if you do specify a number other than zero it ends up being rounded to a set multiple based on your CPUs timing.
None.
Here's a page with just about all you need to know, it does milliseconds, there'd only be an issue if that was too fast for your clock speed, which is basically impossible with computers today.
None.
Quote from name:Zachary Taylor
This is called hysteresis for those wondering. You gotta put in some form of hysteresis when you're measuring something, or else you'll end up with too much data, and burn out your IC.
Totally unrelated topic, man.
'cause monitoring keyboard input is completely different from monitoring a temperature through a thermocouple.
"Parliamentary inquiry, Mr. Chairman - do we have to call the Gentleman a gentleman if he's not one?"
Quote from name:Zachary Taylor
Totally unrelated topic, man.
'cause monitoring keyboard input is completely different from monitoring a temperature through a thermocouple.
Software is completely different from hardware, and the problem he was having was using 99% cpu, not a measurement with too much bounce (which is what hysterisis is for).
None.