如何在 Win 32 上使用 C++ 使程序休眠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2252372/
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
How do you make a program sleep in C++ on Win 32?
提问by Chris_45
How does one "pause" a program in C++ on Win 32, and what libraries must be included?
如何在 Win 32 上“暂停”C++ 中的程序,以及必须包含哪些库?
回答by IVlad
#include <windows.h>
Sleep(number of milliseconds);
Or if you want to pause your program while waiting for another program, use WaitForSingleObject.
或者,如果您想在等待另一个程序时暂停您的程序,请使用WaitForSingleObject。
回答by John Gietzen
If you are using boost, you can use the thread::sleep
function:
如果您使用的是 boost,则可以使用以下thread::sleep
函数:
#include <boost/thread/thread.hpp>
boost::system_time time = boost::get_system_time();
time += boost::posix_time::seconds(1);
boost::thread::sleep(time);
Otherwise, you are going to have to use the win32 api:
否则,您将不得不使用 win32 api:
#include <windows.h>
Sleep(1000);
And, apparently, C++0x includes this:
而且,显然,C++0x 包括:
#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));
回答by Yochai Timmer
In C++11, you can do this with standard library facilities:
在 C++11 中,您可以使用标准库工具执行此操作:
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));
回答by borderless
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
请注意,上面的代码
在 Windows 7上的 Code::Blocks 12.11 和 Visual Studio 2012上进行了测试。
For forcing your programme stop or wait, you have several options :
要强制您的程序停止或等待,您有几种选择:
- sleep(unsigned int)
- 睡眠(无符号整数)
The value has to be a positive integer in millisecond. That means that if you want your programme wait for 2 second, enter 2000.
该值必须是以毫秒为单位的正整数。这意味着如果您希望程序等待 2 秒,请输入 2000。
Here's an example :
这是一个例子:
#include <iostream> //for using cout
#include <stdlib.h> //for using the function sleep
using namespace std; //for using cout
int main(void)
{
cout << "test" << endl;
sleep(5000); //make the programme waiting for 5 secondes
cout << "test" << endl;
sleep(2000); // wait for 2 secondes before closing
return 0;
}
If you wait too long, that probably means the parameter is in second. So change it like that :
如果您等待的时间过长,那可能意味着该参数在第二位。所以改变它:
sleep(5);
For those who get error message or problem using sleep try to replace it by _sleep or Sleep especially on Code::Bloks.
And if you still getting probleme, try to add of one this library on the biggining of the code.
对于那些在使用 sleep 时收到错误消息或问题的人,请尝试将其替换为 _sleep 或 Sleep,尤其是在 Code::Bloks 上。
如果您仍然遇到问题,请尝试在代码变大时添加一个此库。
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
- system("PAUSE")
- 系统(“暂停”)
A simple "Hello world" programme on windows console application would probably close before you can see anything. That the case where you can use system("Pause").
Windows 控制台应用程序上的一个简单的“Hello world”程序可能会在您看到任何内容之前关闭。那就是您可以使用 system("Pause") 的情况。
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
system("PAUSE");
return 0;
}
If you get the message "error: 'system' was not declared in this scope" just add the following line at the biggining of the code :
如果您收到消息“错误:'系统'未在此范围内声明”,只需在代码的大写处添加以下行:
#include <cstdlib>
- cin.ignore()
- cin.ignore()
The same result can be reached by using cin.ignore() :
使用 cin.ignore() 可以达到相同的结果:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.ignore();
return 0;
}
- cin.get()
- cin.get()
example :
例子 :
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
cin.get();
return 0;
}
- getch()
- 获取()
Just don't forget to add the library conio.h :
只是不要忘记添加库 conio.h :
#include <iostream>
#include <conio.h> //for using the function getch()
using namespace std;
int main(void)
{
cout << "Hello world!" << endl;
getch();
return 0;
}
You can have message telling you to use _getch() insted of getch
你可以有消息告诉你使用 _getch() insted of getch
回答by Tronic
If you wish for the program to stay responsive while "paused", you need to use a timer event.
如果您希望程序在“暂停”时保持响应,则需要使用计时器事件。
回答by T.E.D.
It depends on what type of program you are writing.
这取决于您正在编写的程序类型。
A console app can just call Sleep. A GUI app probably does notwant to do this, as all the menus and widgets will go insensitive, and the app won't redraw itself during this period. Instead you need to do something like set yourself up a timer with a callback when it expires.
控制台应用程序只能调用 Sleep。一个GUI应用程序可能并没有想这样做,因为所有的菜单和窗口小部件会不敏感,而在此期间,应用程序将不会重绘自身。相反,您需要做一些事情,例如为自己设置一个在到期时带有回调的计时器。
回答by yan bellavance
Dont use a sleep function in your GUI if it is not provided by the framework you are working with. This could create referencing problems to data (specially in a thread that is not the main thread). This could freeze you GUI. Its not just a question of sleeping for a short time , use waitmutexes if you need to do that.
如果您正在使用的框架未提供睡眠功能,请不要在您的 GUI 中使用睡眠功能。这可能会产生对数据的引用问题(特别是在不是主线程的线程中)。这可能会冻结您的 GUI。这不仅仅是短暂睡眠的问题,如果需要,请使用waitmutexes。