how to make a program stop after a while

This is a discussion on how to make a program stop after a while within the C++ Programming forums, part of the General Programming Boards category; how do i make a segment of program stop after a while, say 30 seconds and give a variable whatever ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    how to make a program stop after a while

    how do i make a segment of program stop after a while, say 30 seconds and give a variable whatever it gets so far?

    thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    The only general solution (and fortunately the easiest if you have access to source code) is to modify the "segment" so it periodically checks elapsed time and, when required, print out the required variables.

    For example, if the segment is executing a loop, add code to the loop body that checks time and prints variables of interest.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by grumpy View Post
    The only general solution (and fortunately the easiest if you have access to source code) is to modify the "segment" so it periodically checks elapsed time and, when required, print out the required variables.

    For example, if the segment is executing a loop, add code to the loop body that checks time and prints variables of interest.
    how do you do it in c++? can you give me an example: like get the start time and the end time, and then get the total elapsed time.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by xiaohuai View Post
    how do you do it in c++? can you give me an example: like get the start time and the end time, and then get the total elapsed time.
    _Mike writes: c++ time
    Google replies: time - C++ Reference

    If you want higher resolution on your timers I think you'd have to use some platform-specific APIs or a 3rd-party library.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    To be quite frank, you simply use a loop of some kind, e.g. a while loop. Pseudocode:
    while now - start_time < 30
    Do_Stuff
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make accept() stop
    By b00l34n in forum Networking/Device Communication
    Replies: 28
    Last Post: 12-20-2004, 05:50 PM
  2. Make Cortana Stop Kicking Me!!!???
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2003, 04:27 PM
  3. how to make a program stop indeffinitely?
    By BrianK in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2002, 11:51 PM
  4. how to make it stop
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 06:43 AM
  5. Make it stop!
    By dougaerb in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2001, 10:04 PM

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