Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Windows (non-standard) Program Pause

Display your little helpful snippets of code that may help us all get something done

Windows (non-standard) Program Pause

Postby antiRTFM on Mon May 12, 2008 3:32 pm

On Microsoft windows, we have the "Sleep" function to pause the program for a given amount of milliseconds. so
Code: Select all
Sleep(1000);
will make the program freeze for 1 second.

EDIT: You will need to #include <windows.h> to use Sleep(). Sometimes its already included through some other headers, sometimes not. To be safe you need to manually include it.
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Postby asib on Wed Jul 23, 2008 12:46 am

Ahhh, very useful to know that this command exists in some type of C++. Thanks antiRTFM ;)
asib
 
Posts: 86
Joined: Tue Jul 22, 2008 9:15 pm

Postby Doctor Salt on Wed Jul 30, 2008 5:54 pm

Just wondering, to get the sleep function working, do you need to include another package/what ever they call them? like
#include <blahblah>?
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Postby Gooberius on Wed Jul 30, 2008 8:20 pm

If you're in Unix/Linux/Mac OS X then you'll need to do

#include <unistd.h>

if you're compiling in Windows then you'll need to do

#include <windows.h>


Oh yes, and the function is

void sleep( unsigned int uSeconds );

under Unix/Linux/Mac OS X, and under Windows it's

void Sleep( DWORD dwMilliseconds );

Watch the capitalization of the S (lower case under *nix etc) and notice that it's seconds in *nix and milliseconds in Windows.
Gooberius
 
Posts: 16
Joined: Mon Jul 28, 2008 7:25 pm

Re: Windows (non-standard) Program Pause

Postby Seato on Fri Jun 26, 2009 8:59 pm

I don't know if this is only for Windows, I just know this method works for windows. It's also very similar to antiRTFM's cin.get() .

You'll need to have #include <conio.h> declared at the top and then you can place getch(); wherever you want to pause until the user presses a key. This is only advantageous if you're planning to have multiple pauses within your programs, it cuts down the amount of keys you may need to type by 2 characters, which may add up to make up far the #include <conio.h> eventually!
User avatar
Seato
 
Posts: 23
Joined: Thu May 21, 2009 5:55 pm

Re: Windows (non-standard) Program Pause

Postby C++ on Sat Jun 27, 2009 4:47 am

Seato wrote:I don't know if this is only for Windows, I just know this method works for windows. It's also very similar to antiRTFM's cin.get() .

You'll need to have #include <conio.h> declared at the top and then you can place getch(); wherever you want to pause until the user presses a key. This is only advantageous if you're planning to have multiple pauses within your programs, it cuts down the amount of keys you may need to type by 2 characters, which may add up to make up far the #include <conio.h> eventually!


Using conio.h is really a bad idea. It's not standard C++, but that's not the biggest reason why it's bad. The implementation of conio.h differs from compiler to compiler. There are situations where you have to use a library to do something the standard doesn't support. If you really have to use a console library use ncurses or Windows console library.
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Windows (non-standard) Program Pause

Postby Seato on Mon Jun 29, 2009 10:27 am

Ah, thanks for the advice C++. It was just something I picked up from the net, better I learn now than later at least.
User avatar
Seato
 
Posts: 23
Joined: Thu May 21, 2009 5:55 pm

Re: Windows (non-standard) Program Pause

Postby tbdawg on Wed Jul 15, 2009 5:57 am

Here is a way to build your own cross platform (at least I think it is?) sleep function which works the same as the Sleep() in <windows.h>:

Code: Select all
#include <ctime>

void sleep(unsigned int mseconds)
{
    clock_t goal= mseconds + clock();
    while( goal > clock() );
}

int main()
{
    sleep(1000);//where 1000 mseconds is equal to 1 sec.
}
tbdawg
 
Posts: 5
Joined: Wed Jul 15, 2009 5:40 am


Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests