Click here to Skip to main content
13,096,399 members (54,158 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 30 Apr 2003

Win32 Schedule Class

, 6 May 2003
Rate this:
Please Sign up or sign in to vote.
Win32 Schedule class for non-critical processes


The Win32 API provides a useful function call SetTimer(), which creates a timer with a specified timeout value. There are however no schedule functions - functions that would trigger periodically at scheduled times.

This article presents CSchedule class usable with command line, Windows and MFC applications.


I was working on a scheduling class for non-critical processes and I wanted the timer to trigger at the minute of the local time - i.e. the timer must trigger at every 10th minute: 10:10am, 10:20am, 10:30am, 10:40am, 10:50am, 11:10am,... - and with the ability to reschedule the trigger time at any instance.

One method was to use CreateWaitableTimer with threads. This proved to be an overkill for non-time-critical tasks. It would be simpler to use CreateWaitableTimer alone. However this function is only available on Win98 and above. I found the SetTime and KillTime functions suitable and much more simple for the task.

Using the code

The CSchedule class is easy to use. There are 3 methods of using CSchedule.

  • Directly calling scheduled function
  • Post scheduled Windows message
  • Overriding ScheduleProcess through inheritance

Directly calling scheduled function

void foo()
    // Do scheduled process here.

// Instantiate object.
CSchedule obj;    
// Function to call on timer.
// Schedule for every 10th minute.
// Start schedule.

In this method, the CSchedule object obj is instantiated and we set the schedule function and the schedule time. To start the schedule object we simply call StartSchedule().

Post scheduled Windows message

// Instantiate object.
CSchedule obj;    
// Set windows message to post.
obj.SetPostMessage(hWnd, WM_USER, IDM_USER, 0);    
// Schedule for every 10th minute.
// Start schedule.

// Windows Callback function.
                        WPARAM wParam, LPARAM lParam)
    switch (message)
        case WM_USER:
                case IDM_USER:
                // Do scheduled process here.

With this method, the schedule object is used to post scheduled Windows messages, which would then be processed on by the Windows callback function.

Posting Windows message can be used with directly calling schedule functions in tandem. Scheduled function will always be called before message is posted.

Overriding ScheduleProcess through inheritance

class CMySchedule : public CSchedule
    virtual ~CMySchedule();
    void foo1();
    void foo2();
    // Overide to call own process.
    virtual VOID ScheduleProcess() {    
    // Do schedule process here.

CMySchedule obj;

// Schedule for every 10th minute.

// Start schedule.

By overriding the ScheduleProcess() method, the child class can implement its own algorithms as shown the code snippets above.

Note: Windows message will still be posted after executing the ScheduleProcess.

Setting Schedule Time and Enumerated Schedule Times

The scheduled time can be set at any instance, even after starting the schedule. The schedule time is expressed in milliseconds. The CSchedule header file defines a few common schedule times (e.g. BY_10_MINUTES, BY_02_HOURS, etc.) which may be of use if you do not want to calculate them yourselves.


While the class went beyond my original intended task, I found it fascinating and hope that it would be of use to other Code Project readers, having benefited much from Code Project myself.


  • April 21, 2003 - First version.
  • May 6, 2003 - Updated downloads base on reader feedbacks


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chiew Heng Wah
Malaysia Malaysia
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralSupport System Hibernation Pin
Blake Miller25-Feb-05 10:33
memberBlake Miller25-Feb-05 10:33 
GeneralRe: Support System Hibernation Pin
Chiew Heng Wah18-Sep-07 16:53
memberChiew Heng Wah18-Sep-07 16:53 
Generalcompiler error Pin
daniellin27-Apr-04 23:52
sussdaniellin27-Apr-04 23:52 
General...Compile error... Pin
MatRicks501-Apr-04 4:00
memberMatRicks501-Apr-04 4:00 
GeneralRe: ...Compile error... Pin
Chiew Heng Wah4-Apr-04 19:47
memberChiew Heng Wah4-Apr-04 19:47 
Questionhow can i schedule function with parameters Pin
ariel biton8-Sep-03 5:47
memberariel biton8-Sep-03 5:47 
AnswerRe: how can i schedule function with parameters Pin
Chiew Heng Wah8-Sep-03 18:10
memberChiew Heng Wah8-Sep-03 18:10 
GeneralRe: how can i schedule function with parameters Pin
ariel biton8-Sep-03 20:25
memberariel biton8-Sep-03 20:25 
QuestionHow to add this class to my appllication Pin
ariel biton11-Aug-03 20:56
memberariel biton11-Aug-03 20:56 
AnswerRe: How to add this class to my appllication Pin
Chiew Heng Wah12-Aug-03 15:20
memberChiew Heng Wah12-Aug-03 15:20 
Generalcompile error!! Pin
liuty20064-May-03 8:46
memberliuty20064-May-03 8:46 
GeneralRe: compile error!! Pin
jhaga4-May-03 10:07
memberjhaga4-May-03 10:07 
GeneralRe: compile error!! - Casting Issue Pin
heng74-May-03 15:26
memberheng74-May-03 15:26 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 7 May 2003
Article Copyright 2003 by Chiew Heng Wah
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid