by compgeek on December 10th, 2003

compgeek

Question

Help answer this question below.

In C/C++, how do you pause a program for a given amount of time using either delay(), sleep(), or some other function?

Answers. 1 helpful answer below.

  • by evanolds on April 28th, 2004

    evanolds

    Although the sleep function is available on multiple platforms, I've only used it in Windows applications. It's usage is fairly simple, it takes 1 paramater that is the delay in milliseconds before the THREAD execution resumes. So if you wanted to pause your thread for 5 seconds (5000 milliseconds), you would use:

    // Start of sample C++ code
    Sleep(5000);
    // End of sample C++ code

    If you're a new-ish programmer and you don't know what a thread is, don't worry about that. Most likely the program you're writing won't be creating many threads so the "Sleep" function will just pause the execution of your entire program for the number of milliseconds that you supply as the parameter.

    You'll need to include <windows.h> to use the Sleep function on a windows application (notice the function has a capital S). I believe on other operating systems the sleep function still has only 1 parameter that represents thread delay in milliseconds but it's defined in a different header and may use a lowercase s.

    No comments. Post one | Permalink

Want to attach an image to your answer? Click here.

Did this answer your question? If not, then ask a new question or create a poll.

More Questions. Additional questions in this category.

You're reading In C/C++, how do you pause a program for a given amount of time using either delay(), sleep(), or some other function?

Follow us on Facebook!

Related Ads

ANSWERBAG BUZZ

C pause
Pause c
Pause in c
C pause thread
C pause execution