Click here to Skip to main content
Click here to Skip to main content
Go to top

C++ Calendar Alarm Class with Repeat Capability

, 23 Mar 2006
Rate this:
Please Sign up or sign in to vote.
CAlarmClock is a C++ class that can generate repeating asynchronous alarms.

Introduction

I recently started working on a part of my application that requires a clock driven scheduler in order to carry out various tasks that the user needs at certain times of the day or week or month. So I decided that I'd try to make the clock calendar alarm component as portable as I can because I wanted to use it in two different apps. Since I tried to make this new class usable in at least two different programs, it wasn't much more effort to share it online. Hopefully others might be able to make use of it too.

This C++ class has the following functionality in order to allow the user to:

  • Set an alarm that will asynchronously alert the application at a later time.
  • Specify a repeating alarm using an arbitrary amount of time, every N days, certain days of the week, or a specific day every month.
  • Have a guaranteed alarm accuracy of just a few milliseconds and have the same accuracy for all subsequent repeating alarms.

Background

In order to measure time, the CAlarmClock class uses the FILETIME structure. It is supposedly accurate within 100 nanoseconds, but the computer hardware will not allow one to measure time units that small. The accuracy is actually much less than 100 nanoseconds, but whatever the accuracy is, it's only as accurate as the code that actually makes the time measurement. I'm not an expert at what the most accurate delay function available is from the various Windows APIs. I know of two ways to measure time: the SetTimer() method that is very popular with me, and the Windows multimedia API function timeSetEvent(). As far as I've read (in the docs) timeSetEvent() is very accurate. I chose to use timeSetEvent as the time base for the main timing loop.

Using the code

The Visual Studio project needs to have the following changes made to it in order to make use of the CAlarmClock class:

  • In Properties | Linker | Input | Additional Dependencies, add the library called "Winmm.lib" to the list for release and debug builds.
  • Properties | C/C++ | Code Generation | Runtime Library should be set so that the program uses the multithreaded run time. The included sample application uses /MD and /MDd for release and debug builds, respectively.
  • #include "AlarmClock.h".
  • Add the following files to your project: "AlarmClock.cpp" and "AlarmClock.h".

The CAlarmClock class has the following public member functions:

  • SetAlarm() stores the caller's alarm date and time into the object's internal alarm variable and sets the alarm to the ARMED state internally and starts timing. The caller's trigger will be delivered when the alarm date and time are reached. There are nine overloads for these functions. Three different input types are accepted for the time, FILETIME (local), SYSTEMTIME (local) and integers. Three signal types are used in the various overloads: Callback, Window Message and Event.
  • SetAlarmAndWait() acts just like the Sleep() function of Windows. It might not be suitable for long delays but it's there to make the object easier to use in some cases. There's no way to abort the alarm once it starts since the thread is tied up waiting for the alarm to be triggered. But another thread can call Abort() if you really want to use it that way. I recommend not using SetAlarmAndWait() except for the most basic timing loops that need a short delay. Also repeated alarms cannot be used with this function.
  • Abort() stops the current alarm and puts the object into a dormant state.
  • IsArmed() allows the caller to ask if the clock is set and armed, or whether it's idle and waiting for another SetAlarm() to be issued. IsArmed will return TRUE if the alarm has been set but the alarm time hasn't been reached yet. After all alarm repeats have been triggered and exhausted, IsArmed will eventually return FALSE.
  • SetRepeat() provides the information needed to schedule one or more automatic repeats. See the REPEAT_PARMS structure (in AlarmClock.h) for information on how to specify a repeated alarm event. There are strict requirements on how to properly set up the REPEAT_PARMS in order to prevent the CAlarmClock object from getting confused. Therefore, SetRepeat() performs validation on the contents of the REPEAT_PARMS parameter. Note: It is wise to call SetRepeat() before calling SetAlarm() but it is not required. I haven't tested whether calling SetRepeat() after SetAlarm() works, but it is supposed to work as long as your alarm time doesn't expire first.
  • ResetRepeats() cancels any future repeat alarms before they occur. The next alarm that is already scheduled to be repeated will still occur when the alarm time is reached. Abort() is the only way to cancel a scheduled alarm that hasn't occurred yet.
  • GetAlarmTime() allows you to retrieve the next alarm time from the object. The resulting FILETIME contents are only valid if the object is in the ARMED state (see IsArmed).
  • New functions: The following SetRepeat_xxx functions are a replacement for the SetRepeat function. Using these new functions eliminates the need to know anything about the REPEAT_PARMS structure. SetRepeat and GetRepeat are still supported for compatibility but using the new SetRepeat_xxx functions (below) is recommended instead.
  • SetRepeat_Interval() replaces SetRepeat() and allows the programmer to set a repeat alarm that is interval based: (days, hours, minutes, seconds and milliseconds).
  • SetRepeat_Monthly() replaces SetRepeat() and allows the programmer to set a repeat alarm that is based on a day of the month.
  • SetRepeat_Weekdays() replaces SetRepeat() and allows the programmer to set a repeat alarm that is one or more weekdays.

    Note: All of the SetRepeat_xxx functions have a parameter (long nCount) which indicates the number of repeats to use. Zero means repeat forever.

  • SetRepeatUntil()
  • GetRepeatUntil()

    Note: The RepeatUntil function takes precedence over the repeat count and repeat forever setting. In other words, calling RepeatUntil will cause the repeat count and repeat forever settings to be ignored. To replace the count, call SetRepeat_xxxx again with a new count value or zero to indicate forever.

None of the functions in this class are virtualized. If you need to subclass, just declare the functions as virtual in the base class if you want to. I just left it all as non-virtual since I have no reason to derive a class from this one myself yet.

If more than one alarm is needed, create more than one CAlarmClock object and keep them all in an array if desired.

Points of interest

All time units are considered local, not UTC. If UTC is desired then you'll need to modify the class to deal with that format and you will need to convert the time to local if you want to know what the local time is. That brings up a point: if this calendar alarm works incorrectly in a different time zone than mine (PST -8) then please let me know so I can set up my dev machine to that zone and try to fix it. Or better yet, modify it and let me have the code so I can update the code in the article.

The class uses a worker thread to handle the time measurement. The worker thread will use the caller's choice (event, message or callback) to signal the caller during the alarm event. If you choose to use the callback function to handle the alarm event, you need to make sure that your callback function does not use much time so that the worker thread can resume measuring time. You should not call any of the member functions of the CAlarmClock class from the callback function because this will cause a deadlock.

There's a bug in my version of Visual Studio .NET 2003 Pro that causes the DateTimePicker control to keep switching back to its default type of date mode instead of time mode, every time I make any edits to the dialog box in the resource editor. Now the control will be updated at run time to force it to become the time format by altering the control's style bits in OnInitDialog.

The Repeat Forever flag in the REPEAT_PARMS structure takes precedence over the repeat counter.

The test app generates a log file in the same path as the executable (or the startup path from the shortcut). The log file is there to help test the alarm repeats. It takes a long time to test a calendar alarm unit in real time so I had no choice but to take shortcuts in testing and make some assumptions regarding whether everything works. If you find any problems, please send me an email, or if you feel the problem is serious enough to tell everyone else then post the bug in the forum for this article if you wish.

New functionality:

  • There is a new repeat end condition (in addition to "repeat forever" and "repeat n times"). It is "repeat until date". The field tRepeatUntil inside the REPEAT_PARMS represents the date/time you want the repeats to end. This field is a union called UFT which makes it easy to convert from FileTime to ULONGLONG. One way to initialize this field is to use a SYSTEMTIME structure as a template. Fill in the blanks then call SystemTimeToFileTime and store the result in REPEAT_PARMS::tRepeatUntil, or you can now call SetRepeatUntil (new, discussed below) and pass it a pointer to the SYSTEMTIME.
  • I added some new functions to eliminate the need to fill in the REPEAT_PARMS yourself. They are: SetRepeat_Interval, SetRepeat_Monthy, SetRepeat_Weekdays and SetRepeatUntil. Now you can simply call one of these to set up the repeat parameters and the CAlarmClock class manages it's own internal copy of the REPEAT_PARMS. You can still use the SetRepeat function as before if desired.
  • The REPEAT_PARMS was changed so that the Repeat_Days mode was eliminated. To repeat for days, you now use the Repeat_Interval mode and fill in the REPEAT_PARMS::dd field (or call the equivalent function called SetRepeat_Interval.

History

  • 07/12/05
    • Article submission.
  • 03/17/06
    • Added functionality and fixed some bugs.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DaveeCom

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembernguyen quang dzung13-May-12 23:17 
GeneralCAlarmClock Applications PinmemberMember 316594717-Apr-09 4:47 
GeneralRepeating Alarm Issue PinmemberExtraLean22-Nov-08 10:45 
GeneralRe: Repeating Alarm Issue PinmemberDaveeCom28-Nov-08 6:55 
GeneralRe: Repeating Alarm Issue PinmemberDaveeCom28-Nov-08 7:23 
JokeRe: Repeating Alarm Issue PinmemberDaveeCom29-Nov-08 7:00 
GeneralRe: Repeating Alarm Issue PinmemberExtraLean22-Dec-08 6:36 
QuestionCould use some help on the callback version of SetAlarm PinmemberBerni Slootbeek1-Oct-08 3:04 
AnswerRe: Could use some help on the callback version of SetAlarm PinmemberDaveeCom1-Nov-08 19:33 
GeneralRe: Could use some help on the callback version of SetAlarm PinmemberBerni Slootbeek9-Nov-08 20:26 
GeneralRe: Could use some help on the callback version of SetAlarm PinmemberDaveeCom15-Nov-08 6:47 
GeneralRe: Could use some help on the callback version of SetAlarm PinmemberBerni Slootbeek2-Dec-08 1:58 
AnswerRe: Could use some help on the callback version of SetAlarm PinmemberDaveeCom19-Dec-08 16:27 
GeneralRe: Could use some help on the callback version of SetAlarm PinmemberBerni Slootbeek10-Feb-09 1:27 
Generaltrying again - Changing the .rc file results in application asking for.dll files PinmemberMalibu046-Sep-08 4:44 
GeneralChecking In PinmemberExtraLean5-Jun-08 12:56 
GeneralRe: Checking In PinmemberExtraLean26-Jul-08 8:48 
GeneralRe: Checking In PinmemberDaveeCom30-Oct-08 17:29 
GeneralRe: Checking In PinmemberExtraLean1-Nov-08 5:15 
GeneralRe: Checking In PinmemberDaveeCom1-Nov-08 8:04 
GeneralRe: Checking In PinmemberExtraLean10-Nov-08 4:37 
GeneralRe: Checking In PinmemberExtraLean22-Nov-08 10:47 
GeneralChanging the .rc file results in application asking for.dll files PinmemberMalibu0417-Dec-07 5:25 
GeneralRe: Changing the .rc file results in application asking for.dll files PinmemberDaveeCom1-Nov-08 19:16 
GeneralGood job! PinmemberOner Dikdere (TR)4-Nov-07 5:21 
Generalthanks Pinmember-=Seph=-5-Jun-07 16:39 
GeneralSetRepeatAlarm issues PinmemberExtraLean9-Mar-07 6:07 
GeneralRe: SetRepeatAlarm issues PinmemberExtraLean9-Mar-07 6:21 
GeneralRe: SetRepeatAlarm issues PinmemberDaveeCom9-Mar-07 9:32 
QuestionRe: SetRepeatAlarm issues PinmemberDaveeCom9-Mar-07 15:32 
AnswerRe: SetRepeatAlarm issues PinmemberExtraLean13-Mar-07 3:42 
GeneralRe: SetRepeatAlarm issues PinmemberDaveeCom6-Apr-07 7:09 
GeneralRe: SetRepeatAlarm issues PinmemberExtraLean13-Mar-07 3:29 
GeneralRe: SetRepeatAlarm issues PinmemberExtraLean13-Mar-07 3:47 
GeneralRe: SetRepeatAlarm issues PinmemberDaveeCom7-Jun-07 2:44 
QuestionWindows XP Only? [modified] PinmemberExtraLean6-Feb-07 7:20 
AnswerRe: Windows XP Only? PinmemberExtraLean7-Feb-07 3:25 
GeneralRe: Windows XP Only? PinmemberILVOTVAT19-Feb-07 4:59 
GeneralRe: Windows XP Only? PinmemberExtraLean19-Feb-07 6:29 
QuestionUpdates, and thanks! PinmemberExtraLean6-Feb-07 7:09 
Generalacess the m_RepaetParms.nRepeat PinmemberILVOTVAT14-Jan-07 4:18 
GeneralRe: acess the m_RepaetParms.nRepeat PinmemberDaveeCom16-Jan-07 13:26 
Generaldll not found when changing caption PinmemberILVOTVAT11-Jan-07 23:09 
GeneralRe: dll not found when changing caption PinmemberDaveeCom14-Jan-07 1:48 
GeneralRe: dll not found when changing caption PinmemberILVOTVAT14-Jan-07 3:51 
QuestionHow can I do something like calculation when the alarm triggered PinmemberPenagihProgramming10-Jan-07 22:45 
AnswerRe: How can I do something like calculation when the alarm triggered PinmemberDaveeCom14-Jan-07 1:37 
QuestionGreat Article, Updates Please? PinmemberExtraLean7-Dec-06 7:15 
AnswerRe: Great Article, Updates Please? PinmemberDaveeCom14-Jan-07 1:40 
GeneralRe: Great Article, Updates Please? PinmemberExtraLean6-Feb-07 7:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 23 Mar 2006
Article Copyright 2005 by DaveeCom
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid