Sleep function in a while statement (basic)

Started by
3 comments, last by Codorke 18 years, 8 months ago
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
Advertisement
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.
____________________________________________________________Programmers Resource Central
Thanks, that was all i had to know.
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.
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.

This topic is closed to new replies.

Advertisement