Thread: Running a procedure repeatedly after a set time interval

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    71

    Running a procedure repeatedly after a set time interval

    I have a certain piece of code that I want to run every 2 minutes. One of my ideas is to get the time and modules that with whatever number represents 2 minutes. Would this work? and is there a simpler way to do accomplish the same thing?

    I think knowing how to do this will come in handy in the future so I am trying to obtain the most elegant solution. Thanks in advance for any help!

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Is this linux or windows? You could create a script or batch file that runs a program every 2 minutes. You could also use Sleep() in your program.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    71
    Oh sorry forgot to specify. It's for windows. I've actually used sleep in a bash script file and just checked that it can be used in c++. Thanks for the help I think that'll work well.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Since you're using windows, you can also put delays in batch files by using the ping command (assuming you're using Windows XP or later). Example batch file fragment that delays for 5 seconds:

    @rem pause for 5 seconds (set n to count + 1)
    @ping 127.0.0.1 -n 6 >nul

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you're doing this in a thread or process that isn't running a message pump (e.g. a command line program) then sleep() is fine.

    If you're doing this in a Windows GUI program (or anything that has a message pump handling messages from the OS), you should register a timer and respond to the timer events.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running time
    By wu_weidong in forum Tech Board
    Replies: 1
    Last Post: 09-05-2005, 04:03 PM
  2. Help !!! Time is running out !!!
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-14-2005, 11:57 AM
  3. Replies: 3
    Last Post: 06-13-2003, 06:47 AM
  4. Replies: 2
    Last Post: 02-28-2002, 12:40 PM
  5. running time
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2002, 09:35 AM