简体   繁体   中英

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

如何在Win 32上用C ++“暂停”一个程序,以及必须包含哪些库?

#include <windows.h>

Sleep(number of milliseconds);

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

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));

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

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

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

如果您希望程序在“暂停”时保持响应,则需要使用计时器事件。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM