# C++: sleep() obsolete... so what's its successor?



## mussavcom

I have a lot of sleep(1) function calls in my program.
sleep(unsigned int) is obsolete, according to the Borland C++ compiler. It gives me the following warning:

Warning W8053 ar1.cpp 26: 'sleep(unsigned int)' is obsolete in function main()

So, OK, it's just a warning, so I keep using sleep(). But now I have too many of these function calls, and I get this message after all the warnings:

Error E2228 ar1.cpp 357: Too many error or warning messages in function DE()

(DE is the last function out of about 6 so far, but later it'll be about 40-50 functions.)

So now it won't compile due to the error of having too many warnings.

Is there a newer function I can use to pause the program for a certain amount of seconds?

Thanks,
Michael


----------



## coderitr

VOID Sleep( DWORD dwMilliseconds ); // in kernel32.lib

Note capital 'S'


----------



## mussavcom

Thanks.. how do I use it? (What's DWORD dwMilliseconds?)


----------



## AlbertB

It means call the function and pass into it the value of the delay you want in milliseconds in the form of a data type DWORD, which is a macro substitution for an "unsigned long" 32bit integer value. You could declare a variable of this type and give it the value of delay you require, or a value dependant on calculation for that matter then just call the Sleep() function and pass that variable to it.

DWORD dwMilliseconds = 1000; // create DWORD type variable to hold delay time in milliseconds
Sleep ( dwMilliseconds ); // *S*leep for that number of msecs, note bold!

try something like that.

Not sure of the '#include's you will need off the top of my head, #include<winbase.h> springs to mind but coderitr would be better advising that if you don't know yourself.


----------



## coderitr

#include <windows.h>

Will get you everything you need. Thanks for the excellent follow-up explanation AlbertB. I copied my post from the MSDN library article on the function without realizing that I may have neede to provide more.

You can call the function without a separate variable declaration ( saving a whopping 4 bytes of memory  ) by the following:

Sleep( 1000 ); // sleep for 1 second


----------



## mussavcom

Thanks, coderitr and AlbertB. Now I can actually pause for half a second, amazing...


----------



## coderitr

Anytime.


----------



## AlbertB

Sorry, I was asleep().


----------

