Results 1 to 7 of 7

Thread: Help with timer function

  1. #1

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

    Help with timer function

    Hi
    I am trying to make my own timer function, but I am having a few problems with it....
    First I create a function

    Code:
    int timer(int t, long current){
          long t2 = (GetTickCount) / 1000;
    
          if(t2-current >= t){
               return t;
    }
    }
    Then I call the function:
    Code:
    timer(2,(GetTickCount)/1000); // pause for 2 seconds
    Where t is the pause time, and current is the time where the function is called.

    But nothing works....what am I doing wrong?
    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
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Help with timer function

    You're lacking a loop that keeps you inside your function until the condition applies. You also need to query the time inside that loop, or else it won't stop.

    Also, the current parameter makes no sense at all and introduces an obscure dependency on another function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

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

    Re: Help with timer function

    I tried a while loop, but never ended....what should I do with the current parameter then? I need t2-t1 = elapsed time, where current is t1, correct?
    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)

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Help with timer function

    This is not working code, it's just to illustrate the concept. You still need to supply a time measuring function (any one will do, but I recommend clock() from the CRT) and its return type.

    Code:
    void busy_delay(unsigned int seconds)
    {
      datatype now = time_func() / TICKS_PER_SECOND;
    
      while(time_func() / TICKS_PER_SECOND - now < seconds) {
        // just wait
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

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

    Re: Help with timer function

    Ok, so its not possible just to use gettickcount, like I did?
    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)

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Help with timer function

    GetTickCount is one of the function you can substitute for my imaginary time_func(). TICKS_PER_SECOND would then equal 1000. I just don't think it's a good choice.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

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

    Re: Help with timer function

    ok, thanks for your help
    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