Point me in the way to a timer?

Discussion in 'C++' started by Djanvk, Mar 11, 2005.

  1. Djanvk

    Djanvk Guest

    Ok I'm working on a program where you have say x number of seconds to
    do something. I'm at a loss on how to go about running a timer
    function. Can anyone point me in the right direction or post a
    snippet of code for me to look at?

    The program I'm trying to write is a 2nd grade math program to help my
    daughter with her addition and subtraction.


    Thanks

    Djanvk
    Djanvk, Mar 11, 2005
    #1
    1. Advertising

  2. Can you be more specific.

    Do you need a way to be interrupted after x seconds?
    OR
    Sleep for x seconds?
    Raghu Uppalli, Mar 11, 2005
    #2
    1. Advertising

  3. "Djanvk" <> wrote in message
    news:...
    > Ok I'm working on a program where you have say x number of seconds to
    > do something. I'm at a loss on how to go about running a timer
    > function. Can anyone point me in the right direction or post a
    > snippet of code for me to look at?
    >
    > The program I'm trying to write is a 2nd grade math program to help my
    > daughter with her addition and subtraction.
    >
    >
    > Thanks
    >
    > Djanvk


    I have some C++ code for a game timer. The interupt is trapped using a
    freind function to the timer class.

    At object creation, the interupt is trapped, the timer is set to 120 ticks
    per second. A static var is used to prevent multiple objects from messing
    up the int handler, and the timing. The int handler increments a public
    field in the object.
    The Freind funct, int handler, and public static count needs to be locked in
    memory to prevent unloading.

    I do not recomend trying to run a great deal of code in the int handler.
    however, I use the timer.count>>4 to synchronize the game at 32 frames per
    sec.

    If you'd like I'll send you a copy of the code, however the version I would
    send is outdated for my purposes, and is untested on most platforms. I used
    it under Windows 95/98. in a 32 bit Protected invironment.

    Dan
    DHOLLINGSWORTH2, Mar 11, 2005
    #3
  4. Djanvk

    osmium Guest

    "Djanvk" writes:

    > Ok I'm working on a program where you have say x number of seconds to
    > do something. I'm at a loss on how to go about running a timer
    > function. Can anyone point me in the right direction or post a
    > snippet of code for me to look at?
    >
    > The program I'm trying to write is a 2nd grade math program to help my
    > daughter with her addition and subtraction.


    A timer is a service provided by the operating system for client programs to
    use. *Your* compiler may list a function named something such as "sleep()".
    But for what you want to do a simpler way is to just burn up some time.
    [sleep() would take control back from your program and give the time to
    someone else to use until the timer expired.]

    Try running this program, experimenting with the input parameter, try an
    initial value such as 5,000. Or, you can be scientific if you wish by using
    the clocks/sec output provided.

    Formally, the type returned by a call on clock() is clock_t, rather than an
    int. But I didn't declare it as such, it gives things an esoteric look I
    would rather avoid in situations such as this..

    #include <iostream>
    #include <ctime>

    using namespace std;

    // burn up n clock cycles
    void delay(int n)
    {
    int now = clock();
    int then = now + n;
    while(now < then)
    // do nothing
    now = clock();
    }
    //===============
    int main()
    {
    cout << "This system advances clock " << CLOCKS_PER_SEC << " times per
    second.\n";
    int cycles;
    cin >> cycles;
    while(1)
    {
    cout << "Start\n";
    delay(cycles);
    cout << "Fin\n";
    }
    }
    osmium, Mar 11, 2005
    #4
  5. Djanvk wrote:
    >
    > Ok I'm working on a program where you have say x number of seconds to
    > do something. I'm at a loss on how to go about running a timer
    > function. Can anyone point me in the right direction or post a
    > snippet of code for me to look at?
    >
    > The program I'm trying to write is a 2nd grade math program to help my
    > daughter with her addition and subtraction.
    >


    If you try to do it the way I think you want, you will get into
    serious troubles.
    What I think you want to implement is this:

    program outputs an assignement
    a timer is started and the program waits for an input
    Now 2 things are possible
    * the timer expires, in which case the assignment counts as not done
    * an input is given
    ...

    Well. The point I am heading at is, that in order to implement it that way
    ou need some sort of interruptable input function. That is something C++
    does not provide out of the box. Also: What should happen, if the user
    starts typing and that timer interrupt occours? Does the answer count
    (because eg. your daughter is just a slower typer and spends half of
    her time in searching for the keys), or does it not count?

    For simplicity I would suggest some slightly change

    program outputs an assignement
    program saves the current time
    program waits for input
    -> (user enters his answer)
    program gets the current time and compares it with the
    previously stored time. If more then x seconds have passed,
    the answer is not counted.
    program checks the answer ...

    For this you only need a function that gives you the current time.
    Look up the functions in
    #include <ctime>
    (the C header would be: #include "time.h" )
    It contains everything you need to implement this strategy.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Mar 11, 2005
    #5
  6. Djanvk

    Djanvk Guest

    yes I want to be able to have it stop putting problems up for my
    daughter to do after a x number of minutes/seconds, like 2 min.

    On 10 Mar 2005 21:11:28 -0800, "Raghu Uppalli" <>
    wrote:

    >Can you be more specific.
    >
    >Do you need a way to be interrupted after x seconds?
    >OR
    >Sleep for x seconds?
    Djanvk, Mar 11, 2005
    #6
  7. Djanvk

    Djanvk Guest

    Thanks I'll give this a run.

    On Fri, 11 Mar 2005 04:47:35 -0800, "osmium" <>
    wrote:

    >"Djanvk" writes:
    >
    >> Ok I'm working on a program where you have say x number of seconds to
    >> do something. I'm at a loss on how to go about running a timer
    >> function. Can anyone point me in the right direction or post a
    >> snippet of code for me to look at?
    >>
    >> The program I'm trying to write is a 2nd grade math program to help my
    >> daughter with her addition and subtraction.

    >
    >A timer is a service provided by the operating system for client programs to
    >use. *Your* compiler may list a function named something such as "sleep()".
    >But for what you want to do a simpler way is to just burn up some time.
    >[sleep() would take control back from your program and give the time to
    >someone else to use until the timer expired.]
    >
    >Try running this program, experimenting with the input parameter, try an
    >initial value such as 5,000. Or, you can be scientific if you wish by using
    >the clocks/sec output provided.
    >
    >Formally, the type returned by a call on clock() is clock_t, rather than an
    >int. But I didn't declare it as such, it gives things an esoteric look I
    >would rather avoid in situations such as this..
    >
    >#include <iostream>
    >#include <ctime>
    >
    >using namespace std;
    >
    >// burn up n clock cycles
    >void delay(int n)
    > {
    > int now = clock();
    > int then = now + n;
    > while(now < then)
    > // do nothing
    > now = clock();
    > }
    >//===============
    >int main()
    > {
    > cout << "This system advances clock " << CLOCKS_PER_SEC << " times per
    >second.\n";
    > int cycles;
    > cin >> cycles;
    > while(1)
    > {
    > cout << "Start\n";
    > delay(cycles);
    > cout << "Fin\n";
    > }
    > }
    >
    Djanvk, Mar 11, 2005
    #7
  8. Djanvk

    Djanvk Guest

    yes basically she has to be able to do x number of math problems in 2
    min's in her class, so I want to program to throw up problems she has
    to solve for 2 minutes, it puts one up and she solves it and it puts
    up another. Thats pretty much it.


    On Fri, 11 Mar 2005 14:34:18 +0100, Karl Heinz Buchegger
    <> wrote:

    >Djanvk wrote:
    >>
    >> Ok I'm working on a program where you have say x number of seconds to
    >> do something. I'm at a loss on how to go about running a timer
    >> function. Can anyone point me in the right direction or post a
    >> snippet of code for me to look at?
    >>
    >> The program I'm trying to write is a 2nd grade math program to help my
    >> daughter with her addition and subtraction.
    >>

    >
    >If you try to do it the way I think you want, you will get into
    >serious troubles.
    >What I think you want to implement is this:
    >
    > program outputs an assignement
    > a timer is started and the program waits for an input
    > Now 2 things are possible
    > * the timer expires, in which case the assignment counts as not done
    > * an input is given
    > ...
    >
    >Well. The point I am heading at is, that in order to implement it that way
    >ou need some sort of interruptable input function. That is something C++
    >does not provide out of the box. Also: What should happen, if the user
    >starts typing and that timer interrupt occours? Does the answer count
    >(because eg. your daughter is just a slower typer and spends half of
    >her time in searching for the keys), or does it not count?
    >
    >For simplicity I would suggest some slightly change
    >
    > program outputs an assignement
    > program saves the current time
    > program waits for input
    > -> (user enters his answer)
    > program gets the current time and compares it with the
    > previously stored time. If more then x seconds have passed,
    > the answer is not counted.
    > program checks the answer ...
    >
    >For this you only need a function that gives you the current time.
    >Look up the functions in
    >#include <ctime>
    >(the C header would be: #include "time.h" )
    >It contains everything you need to implement this strategy.
    Djanvk, Mar 11, 2005
    #8
  9. "Djanvk" <> wrote in message
    news:...
    > yes basically she has to be able to do x number of math problems in 2
    > min's in her class, so I want to program to throw up problems she has
    > to solve for 2 minutes, it puts one up and she solves it and it puts
    > up another. Thats pretty much it.
    >


    The whole timing situation requires that the program not "wait" for input
    Input. This is what you said, this is what your solution is also.

    Under windows, You need a signal comming in to "wake" the program so it can
    check the time.

    otherwise loop you inputs,
    check if info is available,
    if so read it, & proccess it
    check time, has 2 min expired?
    if so signal Semiphore Time Limit Reached.

    Just explain it to the computer.

    Dan
    DHOLLINGSWORTH2, Mar 12, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    702
    Kelsang Wangchuk
    Jul 31, 2003
  2. Replies:
    1
    Views:
    1,572
    Steve C. Orr [MVP, MCSD]
    Feb 22, 2005
  3. Simon

    page timer, or redirect timer

    Simon, Nov 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,180
    Bruce Barker
    Nov 4, 2005
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,285
    Saraswati lakki
    Jan 6, 2012
  5. Replies:
    8
    Views:
    476
    Jorgen Grahn
    Jul 15, 2013
Loading...

Share This Page