I need to sleep my program in Windows. What header file has the sleep function?
Asked
Active
Viewed 2.1e+01k times
4 Answers
110
Use:
#include <windows.h>
Sleep(sometime_in_millisecs); // Note uppercase S
And here's a small example that compiles with MinGW and does what it says on the tin:
#include <windows.h>
#include <stdio.h>
int main() {
printf( "starting to sleep...\n" );
Sleep(3000); // Sleep three seconds
printf("sleep ended\n");
}

Peter Mortensen
- 30,738
- 21
- 105
- 131
-
10"viewed 42374 times" - so obviously many people out there also had this question (and how should I know to prefix "msdn" for my google search when I was mainly doing some Linux/POSIX programming for now?) – mozzbozz Oct 08 '14 at 17:26
8
SleepEx
function (see http://msdn.microsoft.com/en-us/library/ms686307.aspx) is the best choise if your program directly or indirectly creates windows (for example use some COM objects). In the simples cases you can also use Sleep
.

Oleg
- 220,925
- 34
- 403
- 798
0
Include the following function at the start of your code, whenever you want to busy wait. This is distinct from sleep, because the process will be utilizing 100% cpu while this function is running.
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock())
;
}
Note that the name sleep
for this function is misleading, since the CPU will not be sleeping at all.

Cody Piersall
- 8,312
- 2
- 43
- 57

user2876907
- 135
- 1
- 5
-
171. This does not answer the poster's questions 2. This is just wrong because it is still a busy loop (with or without a 'for' loop). – Nico Heidtke Jan 07 '15 at 13:30
-
1up vote. I was searching for some sleep function in windows C, except Sleep(). This is working fine. thanks. – rashok Mar 20 '15 at 16:35
-
12This is not sleep, this is "keep busy in a tight loop", and put extra burden on CPU anyway. Sleep releases the CPU to do other things. – Berin do CD Dec 08 '16 at 18:53
-
up vote too. Althogh it keeps CPU busy but this is much more accurate than `Sleep()`. – ZR Han Jun 09 '22 at 05:21
0
-
1When I include windows.h my compiler gives "error: parameter name omitted" and " error: expected expression before ',' token" – Ariyan Jul 31 '10 at 17:40
-
@Snigger Post a short example that demonstrates this, and tell us what compiler you are using. – Jul 31 '10 at 17:42
-
#include
void writeData(result * res ,char *OUT){ } int main(){ return 1; } gives " error: expected ')' before '*' token" and I'm using GCC (mingw) – Ariyan Jul 31 '10 at 17:48 -
@Snigger Your program does not declare the type "result" - nothing to do with windows or Sleep. and it does not give the error messages you said it did (it gives different ones). – Jul 31 '10 at 17:58
-
-
@Snigger The code in your comment IS the main program. There is no point in posting code that does not illustrate what you are asking about. – Jul 31 '10 at 17:59
-
No I mean I forgot to write the definition of that types in code above but in the other code that gives error those types are defined. – Ariyan Jul 31 '10 at 18:00
-
-
6This is NOT discussion/comments on my answer. This is an entirely tangential discussion that belongs in its own answer. – abelenky Jul 31 '10 at 19:55