Thread: how to make a program stop after a while

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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,318
    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, 06:50 PM
  2. Make Cortana Stop Kicking Me!!!???
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2003, 05:27 PM
  3. how to make a program stop indeffinitely?
    By BrianK in forum C++ Programming
    Replies: 4
    Last Post: 10-29-2002, 12:51 AM
  4. how to make it stop
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 07:43 AM
  5. Make it stop!
    By dougaerb in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2001, 11:04 PM