delaying computer

Programming, for all ages and all languages.
Post Reply
beyondsociety

delaying computer

Post by beyondsociety »

How do I use the "for loop" for delaying the computer?

For instance, I know this freezes the computer:

Code: Select all

for(;;);
Schol-R-LEA

Re:delaying computer

Post by Schol-R-LEA »

Well, a delay loop is simply a loop that does nothing for a fixed large number of repetitions, such as

[tt]for(x=0; x <= 1000000; x++);[/tt]

However, there are two problems with this. First, the delay is dependent on the speed of the processor; the faster the CPU, the shorter the delay would be. You would need to calibrate the delay timings to the speed of the system beforehand, so that on a machine that was twice as fast, it would use twice as many repetitions. In a multitasking system, you would also have to take system loading into consideration.

Second, this sort of delay loop falls into the category of 'busy waiting', in which the CPU is running a taks but not performing any actual work. Again, this presents problems in multitasking systems, as cycles which could be applied to other task get wasted doing nothing.

A far better solution is to use the system delay() or wait() function. Offhand, I can't recall what that would be under Windows, so I will leave it to others to provide.
Tim

Re:delaying computer

Post by Tim »

Under Windows: Sleep(number_of_milliseconds);
beyondsociety

Re:delaying computer

Post by beyondsociety »

Code: Select all

Under Windows: Sleep(number_of_milliseconds);
How would I go about setting this up?
fstream

Re:delaying computer

Post by fstream »

if you want the computer to "freeze" for 500 milliseconds, then just type Sleep(500);
Tim

Re:delaying computer

Post by Tim »

With Sleep() only the calling thread will freeze. The rest of the computer will run as normal. It's actually quite difficult to freeze everything.
Post Reply