473,407 Members | 2,629 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,407 software developers and data experts.

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 16095

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åsdal" 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 µs.

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gmv | last post by:
Hi, I am doing a project and i would like to know if anyone can please tell me how to write a delay or wait function in c++ which works asynchronously with other programs. Thank you, GMV
2
by: notregister | last post by:
I'm to delay my program for some limited time...i try using the following codes but it doesn't work, how do i do a time delay??? 'Extracted from the main function Timer11.Start() Do tCC =...
0
by: jayasakthi | last post by:
when we freeze the columns of a table in jsp, there is a time delay in scrolling the page. when we unlock the columns the time delay is not there. is there any solution for decreasing the time...
1
by: klmishraa79 | last post by:
i want to know how i can put a time delay between two command execution...i.e. after first command of programm the second command should execute after some fixed delay......i want to use time delay...
9
by: Martin Wells | last post by:
I'm doing an embedded systems project and I'm programming it as fully- portable C89 (except of course for setting the pin values). I need to put delays in the program, in the vicinity of 250 ms....
1
by: maul581 | last post by:
I know the delay( ) function of Turbo C and used it in my project. By this function we can give intermediate delay in execution from 1 millisecond to 9999 millisecond. My main question is "Is it...
3
Ali Rizwan
by: Ali Rizwan | last post by:
Hello all, I want to put delay in my loops as we use in c/c++.. Is there any delay function in vb6 which put delay between any code??? Thanx >> ALI <<
2
by: Rahina | last post by:
I am working in Dreamweaver 8, and I have created a simple form: http://www.realmofmystery.com/app.html This is a free website I am doing for some friends, so nothing fancy. And, I learned web...
8
by: Craig Williamson | last post by:
I'm trying to write a time delay loop in Borland C 3.1 (for legacy code) which has an accuracy of about 0.1ms. This change is due to a hardware change and the software is now running too fast to...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.