473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Point me in the way to a timer?

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
Jul 23 '05 #1
8 1726
Can you be more specific.

Do you need a way to be interrupted after x seconds?
OR
Sleep for x seconds?

Jul 23 '05 #2

"Djanvk" <dj****@gmail.com> wrote in message
news:0p********************************@4ax.com...
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
hd***@hotmail.com
Jul 23 '05 #3
"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";
}
}
Jul 23 '05 #4
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
kb******@gascad.at
Jul 23 '05 #5
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" <ra****@rocketmail.com>
wrote:
Can you be more specific.

Do you need a way to be interrupted after x seconds?
OR
Sleep for x seconds?


Jul 23 '05 #6
Thanks I'll give this a run.

On Fri, 11 Mar 2005 04:47:35 -0800, "osmium" <r1********@comcast.net>
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";
}
}


Jul 23 '05 #7
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
<kb******@gascad.at> 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.


Jul 23 '05 #8

"Djanvk" <dj****@gmail.com> wrote in message
news:gi********************************@4ax.com...
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


Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
7
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: -------------...
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
16
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
3
by: raylopez99 | last post by:
Oh, I know, I should have provided complete code in console mode form. But for the rest of you (sorry Jon, just kidding) I have an example of why, once again, you must pick the correct entry point...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.