47

I need to sleep my program in Windows. What header file has the sleep function?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Ariyan
  • 14,240
  • 30
  • 109
  • 168

4 Answers4

104

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,030
  • 21
  • 100
  • 124
  • 9
    "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
  • 219,533
  • 32
  • 395
  • 775
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
  • 7,934
  • 2
  • 38
  • 54
user2876907
  • 127
  • 1
  • 5
  • 16
    1. 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
  • 1
    up 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
  • 11
    This 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. – Bacco Dec 08 '16 at 18:53
0

MSDN: Header: Winbase.h (include Windows.h)

abelenky
  • 61,055
  • 22
  • 102
  • 154
  • 1
    When 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
  • it does! Not in the code above but in the main program it does. – Ariyan 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
  • @Snigger What happens when you compile the code I posted in my answer? –  Jul 31 '10 at 18:01
  • 5
    This 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