1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by mussavcom, Apr 13, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. mussavcom

    mussavcom Thread Starter

    Joined:
    Mar 28, 2004
    Messages:
    51
    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
     
  2. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    VOID Sleep( DWORD dwMilliseconds ); // in kernel32.lib

    Note capital 'S'
     
  3. mussavcom

    mussavcom Thread Starter

    Joined:
    Mar 28, 2004
    Messages:
    51
    Thanks.. how do I use it? (What's DWORD dwMilliseconds?)
     
  4. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    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 ); // Sleep 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. :)
     
  5. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    #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 :D ) by the following:

    Sleep( 1000 ); // sleep for 1 second
     
  6. mussavcom

    mussavcom Thread Starter

    Joined:
    Mar 28, 2004
    Messages:
    51
    Thanks, coderitr and AlbertB. Now I can actually pause for half a second, amazing... :)
     
  7. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    Anytime.
     
  8. AlbertB

    AlbertB

    Joined:
    Nov 24, 2002
    Messages:
    2,438
    Sorry, I was asleep(). ;)
     
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/220086

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice