468,463 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

How to write a time delay function?

Hello,
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

----------------
lovely newsgroup
----------------
Sep 8 '06 #1
24 15780

Chen Shusheng wrote:
Hello,
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

----------------
lovely newsgroup
----------------
usleep() is one way to do it ( whereas it is supported ).

Sep 8 '06 #2
Chen Shusheng said:
Hello,
I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?
#include <time.h>

void Timedelay(float time_lenth)
{
time_t start = time(NULL);
time_t end;
do
{
end = time(NULL);
} while(difftime(end, start) < time_lenth);
}

(Presumably 'lenth' is meaningful somewhere in the world.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #3
Thanks.
"Richard Heathfield" <in*****@invalid.invalid>
??????:XY********************@bt.com...
Chen Shusheng said:
>Hello,
I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

#include <time.h>

void Timedelay(float time_lenth)
{
time_t start = time(NULL);
time_t end;
do
{
end = time(NULL);
} while(difftime(end, start) < time_lenth);
}

(Presumably 'lenth' is meaningful somewhere in the world.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Sep 8 '06 #4
Richard Heathfield <in*****@invalid.invalidwrites:
Chen Shusheng said:
> I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

#include <time.h>

void Timedelay(float time_lenth)
{
time_t start = time(NULL);
time_t end;
do
{
end = time(NULL);
} while(difftime(end, start) < time_lenth);
}

(Presumably 'lenth' is meaningful somewhere in the world.)
That's a portable way to do it, but not a good way to do it. By
calling time() repeatedly in a loop, it's likely (on a
multi-processing system) to gobble lots of CPU time.

Most systems provide a way to do this kind of thing, though standard C
doesn't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 8 '06 #5
"Chen Shusheng" <cs*****@gmail.comwrote:
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?
You use a system-specific function, which you can ask for in a
system-specific newsgroup.
Alternatively, you _could_ use a busy-loop, and be killed by your
systems manager for tying up the system.

Richard
Sep 8 '06 #6

"Richard Bos" <rl*@hoekstra-uitgeverij.nl写入消息新闻:45*****************@news .xs4all.nl...
"Chen Shusheng" <cs*****@gmail.comwrote:
> I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

You use a system-specific function, which you can ask for in a
system-specific newsgroup.
Alternatively, you _could_ use a busy-loop, and be killed by your
systems manager for tying up the system.

Richard
My system is windowsXP. Where should I post to get the answer? Could you pls
indicate?
Sep 8 '06 #7
Keith Thompson said:

<snip>
That's a portable way to do it, but not a good way to do it.
He didn't ask for a good way to do it. By posting in comp.lang.c, he asked
for a comp.lang.c way to do it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #8
Chen Shusheng wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl写入消息新闻:45*****************@news .xs4all.nl...
>"Chen Shusheng" <cs*****@gmail.comwrote:
>> I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?
You use a system-specific function, which you can ask for in a
system-specific newsgroup.
Alternatively, you _could_ use a busy-loop, and be killed by your
systems manager for tying up the system.

Richard

My system is windowsXP. Where should I post to get the answer? Could you pls
indicate?
Start at http://msdn.microsoft.com/ . If you need a newsgroup - query
your news server for windows programming groups.
Sep 8 '06 #9
"Nils O. Sel錽dal" wrote:
Chen Shusheng wrote:
>"Richard Bos" <rl*@hoekstra-uitgeverij.nl>
写入消息新闻:45*****************@news.xs4all.nl.. .
>>"Chen Shusheng" <cs*****@gmail.comwrote:

I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as
"time_lenth"
indicating. Could you help on how to write such a function?
You use a system-specific function, which you can ask for in a
system-specific newsgroup.
Alternatively, you _could_ use a busy-loop, and be killed by your
systems manager for tying up the system.

Richard

My system is windowsXP. Where should I post to get the answer?
Could
you pls indicate?

Start at http://msdn.microsoft.com/ . If you need a newsgroup -
query
your news server for windows programming groups.
The things to look for are SetTimer and KillTimer... These are
Windows-API and thus off topic here. Just to provide a reference for
future lookups. If you need higher resolution use the
multimedia-timer. It's also described there.

HTH
--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.
Sep 8 '06 #10
Richard Heathfield <in*****@invalid.invalidwrites:
Keith Thompson said:

<snip>
>That's a portable way to do it, but not a good way to do it.

He didn't ask for a good way to do it. By posting in comp.lang.c, he asked
for a comp.lang.c way to do it.
No, he asked for a way to do it. If he were knowledgeable enough to
know in detail what's a comp.lang.c question and what isn't, he
wouldn't have had to ask in the first place.

People ask off-topic questions here all the time. The usual response
is to tell the OP that the question is off-topic and suggest a better
place to ask.

To someone who doesn't know C very well, it's not unreasonable to
think that the language might provide a good time delay function.

I can understand giving deliberately bad answers to "do my homework"
questions, but the OP was just looking for information.

In fact, the OP's question was very close to FAQ 19.37, "How can I
implement a delay, or time a user's response, with sub-second
resolution?".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 8 '06 #11
"Chen Shusheng" <cs*****@gmail.comwrites:
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?
The comp.lang.c FAQ is at <http://www.c-faq.com/>. See question
19.37, "How can I implement a delay, or time a user's response, with
sub-second resolution?". (Quick summary: There's no good portable way
to do this, but there are a number of system-specific solutions.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 8 '06 #12
Keith Thompson said:
Richard Heathfield <in*****@invalid.invalidwrites:
>Keith Thompson said:

<snip>
>>That's a portable way to do it, but not a good way to do it.

He didn't ask for a good way to do it. By posting in comp.lang.c, he
asked for a comp.lang.c way to do it.

No, he asked for a way to do it.
He got one. :-)
If he were knowledgeable enough to
know in detail what's a comp.lang.c question and what isn't, he
wouldn't have had to ask in the first place.
Not necessarily true. Plenty of people know what is topical in comp.lang.c
and ask topical questions.
People ask off-topic questions here all the time.
The question was not off-topic.

<snip>
In fact, the OP's question was very close to FAQ 19.37, "How can I
implement a delay, or time a user's response, with sub-second
resolution?".
True enough - although, as you clearly spotted, he actually asked for a
resolution in seconds, rather than anything finer.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #13
On Fri, 8 Sep 2006 17:29:00 +0800, in comp.lang.c , "Chen Shusheng"
<cs*****@gmail.comwrote:
>
My system is windowsXP. Where should I post to get the answer? Could you pls
indicate?
The very first thing you should do is search the MSDN or your compiler
documentation. Typically such functions tell are used to make your
programme "sleep". Thats a hint for the search.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 8 '06 #14
On Fri, 8 Sep 2006 15:57:18 +0200, in comp.lang.c , "John F"
<sp**@127.0.0.1wrote:
>
The things to look for are SetTimer and KillTimer...
Euh, no. but thats why we don't ask or answer system-specific
questions here.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 8 '06 #15
#INCLUDE <WINDOWS.H>

....
SLEEP(1000) /*in milliseconds*/

Sep 8 '06 #16
ni*********@gmail.com writes:
#INCLUDE <WINDOWS.H>

...
SLEEP(1000) /*in milliseconds*/
No.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 8 '06 #17
ni*********@gmail.com wrote:
>
#INCLUDE <WINDOWS.H>

...
SLEEP(1000) /*in milliseconds*/
There is no such directive as #INCLUDE. You may be thinking of
#include. If so, there is no such header as <WINDOWS.H(or
<windows.h>) in standard C. After which there is no such standard
function as SLEEP() in standard C.

Standard C, as described in the various ISO standards, is the
subject of discussion here. Any peculiar non-standard systems are
OFF-TOPIC.

--
"The smaller aperture of the lid has been designed to prevent
hedgehogs from entering the McFlurry container in the
unfortunate incidence that the lid is littered"
-- McDonalds, as quoted in Time, 2006-09-11
--
Posted via a free Usenet account from http://www.teranews.com

Sep 8 '06 #18
Mark McIntyre wrote:
On Fri, 8 Sep 2006 15:57:18 +0200, in comp.lang.c , "John F"
<sp**@127.0.0.1wrote:
>>
The things to look for are SetTimer and KillTimer...

Euh, no.
Why not? :-) It can be done. BTDT. It is as portable and standardized
as any way to achieve this...
but thats why we don't ask or answer system-specific
questions here.
*gg* I know. But if you know better, why not correct it? IMHO under
windows SetTimer and KillTimer are excellent for timing in the range
of milliseconds. They are quite precise too. I even use them in (god
help me here in c.l.c.) VB. In C there are other possibilities but I
usually write a handler for WM_TIMER and things are done. Nothing to
worry any more. If you want a delay, use a lock-variable that is set
on timer start and release it in the callback function. Highest
precision you can imagine under windoze... If you ar not satisfied yet
use the multimedia-timer - then it's even possible to do timing in the
range of 祍.

All not Standard C, all not portable. It just "works". So I'll hop out
of this obviously off-topic stuff here :-)

If you want to shout at me, do so. I have no problem with it.

--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.
Sep 9 '06 #19
"John F" <sp**@127.0.0.1writes:
Mark McIntyre wrote:
>On Fri, 8 Sep 2006 15:57:18 +0200, in comp.lang.c , "John F"
<sp**@127.0.0.1wrote:
>>The things to look for are SetTimer and KillTimer...

Euh, no.

Why not? :-) It can be done. BTDT. It is as portable and standardized
as any way to achieve this...
>but thats why we don't ask or answer system-specific
questions here.

*gg* I know. But if you know better, why not correct it?
[snip]

Because this isn't the place for it.

Assuming SetTimer and KillTimer are specific to MS Windows, there are
a number of Windows-specific newsgroups full of people who know all
about this stuff. comp.lang.c, on the other hand, is constantly
inundated with off-topic posts; carring on off-topic technical
discussions here would only make it worse, and make things more
difficult for those of us who want to talk about C.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 9 '06 #20
John F wrote:
Mark McIntyre wrote:
.... snip ...
>
>but thats why we don't ask or answer system-specific
questions here.

*gg* I know. But if you know better, why not correct it?
Did you never make a mistake, or even a typo? The point is that
the experts on that foreign system are not here, they are on some
other newsgroup. So there is nobody available to point out your
errors, if any. Thus the only sane thing to do is to redirect the
user to the appropriate newsgroup.

--
"The smaller aperture of the lid has been designed to prevent
hedgehogs from entering the McFlurry container in the
unfortunate incidence that the lid is littered"
-- McDonalds, as quoted in Time, 2006-09-11
--
Posted via a free Usenet account from http://www.teranews.com

Sep 9 '06 #21
Keith Thompson wrote:
"John F" <sp**@127.0.0.1writes:
>Mark McIntyre wrote:
>>On Fri, 8 Sep 2006 15:57:18 +0200, in comp.lang.c , "John F"
<sp**@127.0.0.1wrote:
The things to look for are SetTimer and KillTimer...

Euh, no.

Why not? :-) It can be done. BTDT. It is as portable and
standardized
as any way to achieve this...
>>but thats why we don't ask or answer system-specific
questions here.

*gg* I know. But if you know better, why not correct it?
[snip]

Because this isn't the place for it.
He started the fire, not me :-) I just wanted to know why he judged it
"Euh, no".
Assuming SetTimer and KillTimer are specific to MS Windows, there
are
a number of Windows-specific newsgroups full of people who know all
about this stuff. comp.lang.c, on the other hand, is constantly
inundated with off-topic posts; carring on off-topic technical
discussions here would only make it worse, and make things more
difficult for those of us who want to talk about C.
I know the "agenda" of this group.
I just gave a pointer on what he might have a closer look at while
trying to find his way through the (almost) endless fields of the
Windows API :-).

Recently in the thread on accessing the serial port from XP there was
one post on topic to the thread (maybe 2 or 3, don't remember), ~30
complaining that the thread as a whole was not on topic, ~30 about the
response was not on topic or 100% correct (whereas IMHO it was legal
and correct) or that the response to the response....

BTW.:
There are dedicated off-topic NGs (borland.public.off-topic) where a
lot of fun is happening. You should have a look at it. It's moderated
and thus free of spam or rude/hateful stuff.

--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.
Sep 9 '06 #22
CBFalconer wrote:
John F wrote:
>Mark McIntyre wrote:
... snip ...
>>
>>but thats why we don't ask or answer system-specific
questions here.

*gg* I know. But if you know better, why not correct it?

Did you never make a mistake, or even a typo?
Frequently but hardly ever on important stuff. I'm just too careful on
that. Communicating reliable information is not straight forward so I
double check before I actually post something.
The point is that
the experts on that foreign system are not here, they are on some
other newsgroup.
Maybe some are here too, "incognito".
So there is nobody available to point out your
errors, if any.
It was in reply to his
Euh, no.
So I expected him to know something I don't know on that topic (which
may well be true, since I don't know everything).
I just gave some keywords the OP could have a look at, which are on
topic to the subject of this thread.
Thus the only sane thing to do is to redirect the
user to the appropriate newsgroup.

Yes. Has been done. I added keywords for the OP to dig into the
subjecto of timing under windows.

--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.
Sep 9 '06 #23
On Sat, 9 Sep 2006 05:36:41 +0200, in comp.lang.c , "John F"
<sp**@127.0.0.1wrote:
>He started the fire, not me :-) I just wanted to know why he judged it
"Euh, no".
Because its not the way to do what he asked for, but explaining about
it is offtopic here.
>I know the "agenda" of this group.
You put agenda in quotes, like it was something sinister. The only
possible reason for you to do that is becahse you are being
deliberately disingenuous.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 10 '06 #24
Mark McIntyre wrote:
On Sat, 9 Sep 2006 05:36:41 +0200, in comp.lang.c , "John F"
<sp**@127.0.0.1wrote:
>He started the fire, not me :-) I just wanted to know why he judged
it "Euh, no".

Because its not the way to do what he asked for, but explaining
about
it is offtopic here.
It can be done that way.
>I know the "agenda" of this group.

You put agenda in quotes, like it was something sinister. The only
possible reason for you to do that is becahse you are being
deliberately disingenuous.
Um. Maybe :-). I actually like c.l.c.. I still happen to notice the
redundancy of off-topic-calls and even the leagl answers. Bashing on
off-topic stuff shouldn't be stressed that much.

--
Johannes
You can have it:
Quick, Accurate, Inexpensive.
Pick two.
Sep 11 '06 #25

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gmv | last post: by
2 posts views Thread by notregister | last post: by
9 posts views Thread by Martin Wells | last post: by
1 post views Thread by maul581 | last post: by
8 posts views Thread by Craig Williamson | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.