PDA

Click to See Complete Forum and Search --> : Loop with delay or timer function


CyberCarsten
Jan 10th, 2001, 12:09 PM
Hi all!
I want to make a loop that decrease a variable by one each time it loops. This I have covered so far, but I also want the loop to wait 1 or 2 secs. before it loops again.... Is this possible???

parksie
Jan 10th, 2001, 12:13 PM
How accurate does it need to be?

for(int i = 5; i > 0; i--) {
// Do something
Sleep(1000); // Pause for 1 second
}

HarryW
Jan 10th, 2001, 12:39 PM
You can use GetTickCount() to return a long integer containing the number of milliseconds the system has been running. If you check the difference between the current time and a start time you can wait until a second has passed, independant of how long the code in your loop takes to execute (assuming it doesn't take more than 1 second). You can do it like this:


long LastTime = GetTickCount();

for(int i=5; i>0; i--)
{ // Do something
while(GetTickCount() < LastTime + 1000); // Pause for 1 second
LastTime = GetTickCount();
}

parksie
Jan 10th, 2001, 12:41 PM
You can get 1ms resolution by using the multimedia timers.

CyberCarsten
Jan 10th, 2001, 01:56 PM
I think you suggestion will do parksie! Thanks to you and Harry! :)