0

I need to use some form of Sleep() to let the user stay with the speed of the program and watch things move around the screen. I have a problem with using any command that makes the program stop for a small amount of time but at least right now Sleep() seems like my best bet. I have included stdlib.h and accepts _sleep() and when I run the program it says to use Sleep() because its been superceded by a new library. and within stdlib.h Sleep() does exist too. I'm at a complete roadblock any advice is much appreciated! (Different from the 2017 errors because I'm using 2019)

code:

void startGame() {
    //bool flag = true;
    for (int a = 0; a < 10; a++) {
        screenUpdate();
        for (int i = 0; i < 4; i++) {
            rowsShown[i]++;
        }
        Sleep(500); // this comes out with an error
        //to show what the other one does
        _sleep(500);
    }
}

Errors:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0020   identifier "Sleep" is undefined 

Severity    Code    Description Project File    Line    Suppression State
Error   C4996    '_sleep': This function or variable has been superceded by newer library or operating system functionality. Consider using Sleep instead. See online help for details.

Image of the errors

Image of the code

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 10
    Please post code and errors as text, note pictures. Not everyone can see pictures. – NathanOliver Nov 12 '19 at 17:54
  • 1
    how to sleep: https://stackoverflow.com/questions/4184468/sleep-for-milliseconds (note that the accepted answer is outdated, there is another one for c++11) – 463035818_is_not_an_ai Nov 12 '19 at 17:56
  • 7
    I would use this instead: [https://en.cppreference.com/w/cpp/thread/sleep_for](https://en.cppreference.com/w/cpp/thread/sleep_for) the reason is it makes your code more portable. – drescherjm Nov 12 '19 at 17:56
  • 1
    For the future: error information copy from "output" window. In VS quite often there is usuful information not shown in error window. – Marek R Nov 12 '19 at 18:02
  • 2
    Well, a good place to start is are you sure you are including the correct header for [`Sleep()`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep)? –  Nov 12 '19 at 18:05
  • @drescherjm this seems to work! thanks ill use this instead. – Dominik Wylie Nov 12 '19 at 18:11
  • @Chipster yeah i have `#include ` there at the top – Dominik Wylie Nov 12 '19 at 18:34
  • 1
    [This](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep) says you need [`synchapi.h`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/) –  Nov 12 '19 at 19:23
  • Possible duplicate of [Visual Studio 2017 errors on standard headers](https://stackoverflow.com/questions/42777424/visual-studio-2017-errors-on-standard-headers) – Marek R Nov 13 '19 at 10:46
  • @Chipster ah thanks that's it thanks! Want to post it as the answer? – Dominik Wylie Nov 13 '19 at 16:12
  • It's _not_ the answer. The documentation says the function is declared in synchapi.h, but that you should include Windows.h. And this is why we don't post answers in comments. – Lightness Races in Orbit Nov 13 '19 at 16:33
  • @LightnessRaceswithMonica So you're saying the OP needs to include `Windows.h`? If so, wouldn't that be an answer? –  Nov 13 '19 at 19:21
  • @Chipster Yes, it would - unfortunately you forced me to provide my feedback on your answer in the same place you posted it! – Lightness Races in Orbit Nov 13 '19 at 19:33

1 Answers1

1

It is suggested to include windows.h (#include <windows.h>) instead of just include synchapi.h because

There are a number of child header files that are automatically included with windows.h. Many of these files cannot simply be included by themselves (they are not self-contained), because of dependencies.

So if you just include synchapi.h you may get more E0020 errors that other identifiers are undefined.

You can refer to "Using Thread Local Storage" sample for how to use Sleep function.

If you want Faster Builds with Smaller Header Files you can reduce the size of the Windows header files by excluding some of the less common API declarations. More detailed information you can find at "Using the Windows Headers".

Rita Han
  • 9,574
  • 1
  • 11
  • 24
  • It is better to just use `std::this_thread::sleep_for(std::chrono::milliseconds{500})`. It is more portable and there is no need to include `windows.h` – Mestkon Nov 14 '19 at 07:49