Thread: timing issue

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

    timing issue

    Hi, im trying to make a C++ program run at a certain time, lets say 6.00pm. To do this, do i need to add a certain code to my c++ code, or does it do with the operating system. Could someone tell me how??
    Im running Windows Xp Home edition, Thanks...

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is the easiest way to perform operations that are based on some specific future time:

    First, you'll need to include the windows.h library:
    Code:
    #include<windows.h>

    SYSTEMTIME is a structure containing the following attributes:
    Code:
    typedef struct _SYSTEMTIME
    {
         WORD wYear;
         WORD wMonth;
         WORD wDayOfWeek;
         WORD wDay;
         WORD wHour;
         WORD wMinute;
         WORD wSecond;
         WORD wMilliseconds;
    }SYSTEMTIME, *PSYSTEMTIME;
    Using the SYSTEMTIME structure, it is really easy to get any information we want as far as the current time. The month is 1-based (that is, January is 1), and the day of the week is 0-based (Sunday is 0). The wDay field is the current day of the month, which is also 1-based.

    Now declare an object of type SYSTEMTIME:
    Code:
    SYSTEMTIME st;
    GetLocalTime() will populate the SYSTEMTIME struct with the most current local date and time directly from the user's computer. It accepts a single argument:
    Code:
    GetLocalTime(&st);
    Also, we have GetSystemTime()... similar to GetLocalTime, but will populate the SYSTEMTIME struct with the current UTC time and date.
    Code:
    GetSystemTime(&st);
    At this point, you know how to obtain the current time and date. This might be enough for what you are looking to do. You can simply compare current time and date to that of a specific point in the future, and then perform an action when they are equal. An alternative method would be to set a timer and perform an action when the timer expires, but setting a timer would require a background in windows programming.. having your program respond to WM_TIMER messages that would be send to your program by windows at a predetermined intervals.
    Last edited by The Brain; 04-13-2005 at 09:32 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    You can do it one of two ways.

    1) Write code into your program which will check the current system time every X seconds, and when the time is 6pm, execute whatever it is you need to do.

    2) Write your program so that it executes the desired code as soon as it's run, and then set up a schedule to run your program at 6pm. For more information on how to do this, check out the "at" command which is supported on Windows XP.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  3. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  4. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 12:43 AM