954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help writing a delay function in C++

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. :confused: Thank you for ur help.

gmv

gmv
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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. :confused: Thank you for ur help.

gmv

Use Sleep( int waitTime ) in windows. It has only a resolution of 1 millisecond however. Dont know about cases when you want to wait for shorter times.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
Use Sleep( int waitTime ) in windows. It has only a resolution of 1 millisecond however. Dont know about cases when you want to wait for shorter times.

Hi,
Thank you very much for your reply.
i did use Sleep(20) but it doens't seem to work. Is there a wrong or a right way to do it? i am also wondering if the sleep fnc asynchronous?

gmv

gmv
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 
Hi, Thank you very much for your reply. i did use Sleep(20) but it doens't seem to work. Is there a wrong or a right way to do it? i am also wondering if the sleep fnc asynchronous? gmv

It should work. The function will wait for at least 20 milliseconds. Was that the time you wanted the function to wait? THe Sleep function is independent of the other Processes or threads. It makes the calling thread wait for at least the time specified.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Since it's in milliseconds, you might want to do something like 5000 to notice it. In unix, it's lowercase sleep, and it's in seconds..

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 
Hi, Thank you very much for your reply. i did use Sleep(20) but it doens't seem to work. Is there a wrong or a right way to do it? i am also wondering if the sleep fnc c? gmv

What makes you think it doesn't work? 20 milliseconds is not a very long time :eek: During the sleep period you program get no cpu time because the os's thread scheduler does not give it any. However, MS-Windows os is not rocket science, so don't expect the sleep period to be very exact -- meaning that the program may sleep anywhere from 1 to 200 or so milliseconds.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ya you might want to include , it works for me anyway .

tdizzle342
Newbie Poster
19 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

Let sleeping threads lie.

If you must open a new thread, [thread=35517]refer back to the original[/thread].

But then you might realize that a new thread would be relatively pointless, much like replying to a dead thread.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You