waiting

This is a discussion on waiting within the C++ Programming forums, part of the General Programming Boards category; how can I get my program to wait for a second before it does the next command? I tried for(i=0; ...

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    Question waiting

    how can I get my program to wait for a second before it does the next command?

    I tried
    for(i=0; i<10000; i++) {}
    but on a pentium 3/1000mhz it doesn't slow it down at all. surely there is a better way.
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    what compiler?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    microsoft vc++
    My site to register for all my other websites!
    'Clifton Bazaar'

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    you need to include stdlib.h
    and call this function where you want it to wait

    _sleep( numberofmillisecondsgoeshere );
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    thanks, I have 4 text books and tried to look for 'wait' or 'sleep' but couldn't find anything.
    My site to register for all my other websites!
    'Clifton Bazaar'

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by phantom
    thanks, I have 4 text books and tried to look for 'wait' or 'sleep' but couldn't find anything.
    As you have MSVC, you may wish to wander down the WinAPI road;

    Code:
    Sleep
    The Sleep function suspends the execution of the current thread for a specified interval. 
    
    VOID Sleep(
      DWORD dwMilliseconds   // sleep time in milliseconds
    );
     
    Parameters
    dwMilliseconds 
    Specifies the time, in milliseconds, for which to suspend execution. 
    A value of zero causes the thread to relinquish the remainder of its time slice to any other thread 
    of equal priority that is ready to run. 
    If there are no other threads of equal priority ready to run, the function returns immediately, 
    and the thread continues execution. A value of INFINITE causes an infinite delay. 
    Return Values
    This function does not return a value.
    From MSDN

    Dont forget the <windows.h>

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    917
    Not to the millisecond, but works with everthing.

    PHP Code:
    #include <time.h>

    int wait(int SecondsToWait)
    {
        
    time_t TheTime time(NULL);
        while(
    time(NULL) - TheTime SecondsToWait);
        return 
    1;

    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    cheers all, I'll try the differant ways and see what works best.

    quick question for code monkey -> what's the reason for 'return 1'?

    It may be a stupid question but i'm asking it anyway
    Last edited by phantom; 01-28-2002 at 07:00 AM.
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  2. Waiting For A Thread To Idle
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 10-10-2008, 07:31 PM
  3. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  4. Getting input without waiting for keypress
    By Ink in forum C Programming
    Replies: 4
    Last Post: 10-01-2004, 02:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21