How do you make a program sleep in C++ on Win 32?

2019-01-23 05:40发布

问题:

How does one "pause" a program in C++ on Win 32, and what libraries must be included?

回答1:

#include <windows.h>

Sleep(number of milliseconds);

Or if you want to pause your program while waiting for another program, use WaitForSingleObject.



回答2:

If you are using boost, you can use the thread::sleep function:

#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:

#include <windows.h>
Sleep(1000);

And, apparently, C++0x includes this:

#include <thread>
std::this_thread::sleep_for(chrono::seconds(1));


回答3:

If you wish for the program to stay responsive while "paused", you need to use a timer event.



回答4:

In C++11, you can do this with standard library facilities:

#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));


回答5:

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.



回答6:

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.



回答7:

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 :


  • 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.

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.

#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").

#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()

The same result can be reached by using cin.ignore() :

#include <iostream>     

using namespace std;    

int main(void)         
{
    cout << "Hello world!" << endl;

    cin.ignore();

    return 0;
}

  • 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 :

#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



标签: c++ sleep