Staredit Network > Forums > Technology & Computers > Topic: Freezing a Process
Freezing a Process
Mar 29 2009, 12:05 am
By: Falkoner  

Mar 29 2009, 12:05 am Falkoner Post #1



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:

Code
#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.

Mar 29 2009, 2:46 am A_of-s_t Post #2

aka idmontie

You could use:

Code
system("pause");




Personal GitHub
Starcraft GitHub Organization - Feel free to request member status!
TwitchTV

Mar 29 2009, 4:56 am Falkoner Post #3



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.

Mar 29 2009, 5:39 am A_of-s_t Post #4

aka idmontie

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 .



Personal GitHub
Starcraft GitHub Organization - Feel free to request member status!
TwitchTV

Mar 29 2009, 7:52 am ShadowFlare Post #5



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.

Mar 29 2009, 11:14 am Falkoner Post #6



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.

Mar 29 2009, 1:20 pm ShadowFlare Post #7



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.

Mar 29 2009, 4:28 pm O)FaRTy1billion[MM] Post #8

👻 👾 👽 💪

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!
\:farty\: This page has been viewed [img]http://farty1billion.dyndns.org/Clicky.php?img.gif[/img] times!

Mar 29 2009, 7:24 pm Falkoner Post #9



Ah, you're right, it's surprising how much it helps the processor, thanks guys :)



None.

Mar 29 2009, 10:25 pm rockz Post #10

ᴄʜᴇᴇsᴇ ɪᴛ!

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?"

Mar 30 2009, 12:12 am Doodle77 Post #11



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.

Mar 30 2009, 12:19 am Falkoner Post #12



Yeah.. I was gonna say.. :P

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.

Mar 30 2009, 3:16 am Elvang Post #13



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.

Mar 30 2009, 3:36 am Falkoner Post #14



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.

Mar 30 2009, 7:28 pm rockz Post #15

ᴄʜᴇᴇsᴇ ɪᴛ!

Quote from Doodle77
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?"

Mar 30 2009, 10:13 pm Doodle77 Post #16



Quote from name:Zachary Taylor
Quote from Doodle77
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.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:19 pm]
Vrael -- IM GONNA MANUFACTURE SOME SPORTBALL EQUIPMENT WHERE THE SUN DONT SHINE BOY
[2024-5-02. : 1:35 am]
Ultraviolet -- Vrael
Vrael shouted: NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
Gonna put deez sportballs in your mouth
[2024-5-01. : 1:24 pm]
Vrael -- NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
[2024-4-30. : 5:08 pm]
Oh_Man -- https://youtu.be/lGxUOgfmUCQ
[2024-4-30. : 7:43 am]
NudeRaider -- Vrael
Vrael shouted: if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
Yeah I'm not a big fan of Westernhagen either, Fanta vier much better! But they didn't drop the lyrics that fit the situation. Farty: Ich bin wieder hier; nobody: in meinem Revier; Me: war nie wirklich weg
[2024-4-29. : 6:36 pm]
RIVE -- Nah, I'm still on Orange Box.
[2024-4-29. : 4:36 pm]
Oh_Man -- anyone play Outside the Box yet? it was a fun time
[2024-4-29. : 12:52 pm]
Vrael -- if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
[2024-4-29. : 11:17 am]
Zycorax -- :wob:
[2024-4-27. : 9:38 pm]
NudeRaider -- Ultraviolet
Ultraviolet shouted: NudeRaider sing it brother
trust me, you don't wanna hear that. I defer that to the pros.
Please log in to shout.


Members Online: Moose