Results 1 to 5 of 5

Thread: Loop with delay or timer function

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Cool

    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???
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How accurate does it need to be?
    Code:
    for(int i = 5; i > 0; i--) {
        // Do something
        Sleep(1000); // Pause for 1 second
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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:

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

    "From one thing, know ten thousand things."

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can get 1ms resolution by using the multimedia timers.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    I think you suggestion will do parksie! Thanks to you and Harry!
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width