How does one "pause" a program in C++ on Win 32, and what libraries must be included?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
It depends on what type of program you are writing.
A console app can just call Sleep. A GUI app probably does not want 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.
If you wish for the program to stay responsive while "paused", you need to use a timer event.
In C++11, you can do this with standard library facilities:
Please note that the code above was tested on Code::Blocks 12.11 and Visual Studio 2012
on Windows 7.
For forcing your programme stop or wait, you have several options :
The value has to be a positive integer in millisecond. That means that if you want your programme wait for 2 second, enter 2000.
Here's an example :
If you wait too long, that probably means the parameter is in second. So change it like that :
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.
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").
If you get the message "error: 'system' was not declared in this scope" just add the following line at the biggining of the code :
The same result can be reached by using cin.ignore() :
example :
Just don't forget to add the library conio.h :
You can have message telling you to use _getch() insted of getch
If you are using boost, you can use the
thread::sleep
function:Otherwise, you are going to have to use the win32 api:
And, apparently, C++0x includes this:
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.