Time Delay

This is a discussion on Time Delay within the C++ Programming forums, part of the General Programming Boards category; I was wondering if anyone had a code for a time delay. What im trying to do is make a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    Time Delay

    I was wondering if anyone had a code for a time delay. What im trying to do is make a program to help out my Americas Army gameing clan to execute scripts that protect against hackers. What i need to even start off the program is a part that will delay the time for 8 seconds before executing another script. it is important to have a delay or else they will all run at the same time and it wont even work. Any help is appreciated.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    What you're looking for is called a sleep function, but it is not standard. What platform will the be running on? You can do a board search to find some advice on multiplatform APIs containing sleep functions, or search google.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    This question has been asked many times before, a board search for 'delay' or 'time delay' will probably yield you plenty of info. A quick method is:
    Code:
    #include <windows.h>
    ...
    Sleep(8000);
    **EDIT**
    Of course, this is assuming you're running Windows
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    It will be running off of a windows xp platform, what will happen is the game will call the program and then the program will call the scripts every 8 seconds or so.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    In that case, Sleep() is probably the most straightforward method to use. Otherwise, if you care to set up a Win32 API application, you can set a timer to run scripts every 8 seconds.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Quote Originally Posted by Hunter2
    This question has been asked many times before, a board search for 'delay' or 'time delay' will probably yield you plenty of info. A quick method is:
    Code:
    #include <windows.h>
    ...
    Sleep(8000);
    **EDIT**
    Of course, this is assuming you're running Windows
    now im new to this sleep function would i put sleep before or after each script.

    example:

    script one
    sleep
    script two

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Then Hunter2's method should work. MSDN is a very good reference for how to do things under Windows, but the best method of searching it is actually to use google, but add MSDN to your search terms.

    edit: When the function is called, the next line of code isn't executed until the delay is up, so you should put the delay between the two functions you want to put a pause in between.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    All Sleep() does is delay execution for the specified number of milliseconds (1/1000 second). Where you decide to delay is completely up to you.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    ok i did a test with sleep and when i went to compile it said 'sleep' : undeclared identifier and it wont finish any reasons why?

  10. #10
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    #include <windows.h>
    ...
    Sleep(8000);
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  11. #11
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    if bolding means i didnt include the include then it still wont work because i did included windows.h and its not working. if it helps im using VC++6.0

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Then this code should work for you.

    Code:
    #include <iostream>
    #include <windows.h>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main() {
    
        cout <<"This is a message before a wait of 2 seconds" << endl;
        Sleep(2000);
        cout << "This should be 2 seconds later"<<endl;
    
        cin.get();
        return 0;
    }
    Note the capital S
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    thanks andyhunter that worked. thanks to the rest of you guys for the quick responses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. Time delay function
    By sundeeptuteja in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2003, 04:17 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

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