# Sleep function in a while statement (basic)

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can i do following statement in C++ :

while( ... ) {
doFunction(...);
// sleep for x millisec so doFunction( ... ) can proceed x millisec without
// beeing re-called during these time
}


I know this is basic procedure but i never used a sleep function before Thanks

##### Share on other sites
Using windows I think there is a function Sleep(int milliseconds), for which you have to include windows.h (I think).
If you don't want to rely on any other functions then in your sleep function you will have to loop untill a certain time has passed.

##### Share on other sites
Thanks, that was all i had to know.

##### Share on other sites
If you do it in that fashion the sleep will not run until doFunction() has returned anyway, so you will be executing the function in its entirety (unless it does threading stuff inside) and then sleeping for however long, so the delay will always come after the function call.

You don't mention your thread situation though so I'm just assuming you are running in a single thread, so everything will proceed sequentially.

##### Share on other sites
I needed it to turn several pages in my 3D Book. When i click on a bookmark the pages turn too the specified page. But my pages may not turn all together. So i needed somehow something to delay the turn operation between 2 pages. But if i use the sleep function my entire program falls a sleep and the turn action from the previous page also stops executing.

So i use a flag that will be set when the previous page is turned 5 degrees. Until the flag is set no other page can be turned. And the function to turn the pages will be called until the specified page is reached.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633317
• Total Posts
3011336
• ### Who's Online (See full list)

There are no registered users currently online

×