windows 如何在 MSVC++ 控制台应用程序中休眠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/726979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:16:07  来源:igfitidea点击:

How do I sleep in my MSVC++ console application?

windowsvisual-c++consoleconsole-applicationsleep

提问by 108

I want to call Sleep(x), where x is milliseconds to sleep. I know this is the function, but it doesn't work for me. What do I need to include?

我想调用 Sleep(x),其中 x 是睡眠的毫秒数。我知道这是功能,但它对我不起作用。我需要包括什么?

---SOLVED---

- -解决了 - -

I was using the compiler option /Za, which disabled the native Windows extensions used in Sleep()'s implementation.

我使用了编译器选项 /Za,它禁用了 Sleep() 实现中使用的本机 Windows 扩展。

回答by aJ.

SleepUsing WINAPI Sleep(__in DWORD dwAtleastFor8HoursForGoodHealth);

睡眠使用WINAPI睡眠(__在DWORD dwAtleastFor8HoursForGoodHealth);

Include Winbase.h or Windows.h

包括 Winbase.h 或 Windows.h

回答by T.E.D.

Sleep is defined in Winbase.h, but you should include Windows.h to get it.

睡眠在 Winbase.h 中定义,但您应该包含 Windows.h 以获取它。

In the future you can find your information for yourself by bringing up the Help from VisualStudio or searching MSDN. All such info on Win32 API calls should be in there.

将来,您可以通过从 VisualStudio 调出帮助或搜索 MSDN 来查找自己的信息。Win32 API 调用的所有此类信息都应该在那里。

回答by Serge Wautier

you just Sleep(asManyMilliSecondsAsYouWant);

你只是睡觉(asManyMilliSecondsAsYouWant);

回答by Michael

Sleep(int milliseconds)is a Win32 API to suspend execution of your program for the set number of milliseconds.

Sleep(int milliseconds)是一个 Win32 API,用于在设定的毫秒数内暂停程序的执行。

Include <windows.h>to get access to it.

包括<windows.h>以访问它。

MSDN usually lists what header files and libraries API's are in.

MSDN 通常会列出 API 所在的头文件和库。

回答by Anthony

This might be a bit too late (for the 'asker' :)), but in MSVC (2010) (console application) the sleep is in SECONDS not Milliseconds.

这可能有点太晚了(对于“提问者”:)),但在 MSVC(2010)(控制台应用程序)中,睡眠时间是秒而不是毫秒。