Page 1 of 1

delaying computer

Posted: Fri Mar 07, 2003 7:55 pm
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(;;);

Re:delaying computer

Posted: Sat Mar 08, 2003 2:07 am
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.

Re:delaying computer

Posted: Sat Mar 08, 2003 7:09 am
by Tim
Under Windows: Sleep(number_of_milliseconds);

Re:delaying computer

Posted: Tue Mar 11, 2003 9:47 pm
by beyondsociety

Code: Select all

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

Re:delaying computer

Posted: Tue Mar 11, 2003 10:07 pm
by fstream
if you want the computer to "freeze" for 500 milliseconds, then just type Sleep(500);

Re:delaying computer

Posted: Wed Mar 12, 2003 1:28 pm
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.