|
-
Dec 15th, 2004, 10:35 AM
#1
Thread Starter
Frenzied Member
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?
-
Dec 15th, 2004, 10:47 AM
#2
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.
-
Dec 15th, 2004, 10:50 AM
#3
Thread Starter
Frenzied Member
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?
-
Dec 15th, 2004, 11:40 AM
#4
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.
-
Dec 15th, 2004, 12:46 PM
#5
Thread Starter
Frenzied Member
Re: Help with timer function
Ok, so its not possible just to use gettickcount, like I did?
-
Dec 15th, 2004, 01:10 PM
#6
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.
-
Dec 16th, 2004, 12:25 AM
#7
Thread Starter
Frenzied Member
Re: Help with timer function
ok, thanks for your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|