Click here to Skip to main content
Click here to Skip to main content

Win32 Schedule Class

By , 6 May 2003
Rate this:
Please Sign up or sign in to vote.

Introduction

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.

Background

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.
obj.SetScheduleFunction(foo);    
// Schedule for every 10th minute.
obj.SetScheduleTime(BY_10_MINUTES);    
// Start schedule.
obj.StartSchedule();    

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.
obj.SetScheduleTime(BY_10_MINUTES);    
// Start schedule.
obj.StartSchedule();    

// Windows Callback function.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
                        WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_USER:
            switch(wParam)
            {
                case IDM_USER:
                // Do scheduled process here.
                break;
            }
            break;
    }
}

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
{
public:
    CMySchedule();
    virtual ~CMySchedule();
    void foo1();
    void foo2();
protected:
    // Overide to call own process.
    virtual VOID ScheduleProcess() {    
    // Do schedule process here.
        foo1();
        foo2();
    }
};

CMySchedule obj;

// Schedule for every 10th minute.
obj.SetScheduleTime(BY_10_MINUTES);  

// Start schedule.
obj.StartSchedule();

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.

Conclusion

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.

History

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

License

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
Other
Malaysia Malaysia
No Biography provided

Comments and Discussions

 
GeneralSupport System Hibernation PinmemberBlake Miller25-Feb-05 10:33 
GeneralRe: Support System Hibernation PinmemberChiew Heng Wah18-Sep-07 16:53 
Generalcompiler error Pinsussdaniellin27-Apr-04 23:52 
General...Compile error... PinmemberMatRicks501-Apr-04 4:00 
GeneralRe: ...Compile error... PinmemberChiew Heng Wah4-Apr-04 19:47 
Questionhow can i schedule function with parameters Pinmemberariel biton8-Sep-03 5:47 
AnswerRe: how can i schedule function with parameters PinmemberChiew Heng Wah8-Sep-03 18:10 
GeneralRe: how can i schedule function with parameters Pinmemberariel biton8-Sep-03 20:25 
QuestionHow to add this class to my appllication Pinmemberariel biton11-Aug-03 20:56 
AnswerRe: How to add this class to my appllication PinmemberChiew Heng Wah12-Aug-03 15:20 
Generalcompile error!! Pinmemberliuty20064-May-03 8:46 
GeneralRe: compile error!! Pinmemberjhaga4-May-03 10:07 
GeneralRe: compile error!! - Casting Issue Pinmemberheng74-May-03 15:26 

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
Web02 | 2.8.140415.2 | Last Updated 7 May 2003
Article Copyright 2003 by Chiew Heng Wah
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid