Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

sleep

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

like image 787
Chris_45 Avatar asked Feb 12 '10 14:02

Chris_45


People also ask

Is there a sleep command in C?

The sleep() method in the C programming language allows you to wait for just a current thread for a set amount of time. The sleep() function will sleep the present executable for the time specified by the thread.

How does sleep () work in C?

The sleep() function in C returns 0 if the requested time has elapsed. Due to signal transmission sleep() returns the unslept quantity, a difference between the requested time to sleep() and the time it actually slept in seconds.

What header is sleep in C?

Answer: The header for sleep is “unistd. h” for LINUX/UNIX Operating system and “Windows. h” for the Windows Operating system.

How do you make a program sleep in C++?

h> Sleep(number of milliseconds); Or if you want to pause your program while waiting for another program, use WaitForSingleObject.


2 Answers

#include <windows.h>  Sleep(number of milliseconds); 

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

like image 127
IVlad Avatar answered Sep 28 '22 18:09

IVlad


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

#include <chrono> #include <thread> std::this_thread::sleep_for(std::chrono::milliseconds(x)); 
like image 31
Yochai Timmer Avatar answered Sep 28 '22 18:09

Yochai Timmer