Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C++
Article

Win32 Schedule Class

Rate me:
Please Sign up or sign in to vote.
2.92/5 (10 votes)
6 May 20032 min read 90.3K   2.2K   29   13
Win32 Schedule class for non-critical processes

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


Written By
Malaysia Malaysia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSupport System Hibernation Pin
Blake Miller25-Feb-05 10:33
Blake Miller25-Feb-05 10:33 
GeneralRe: Support System Hibernation Pin
Chiew Heng Wah18-Sep-07 16:53
Chiew Heng Wah18-Sep-07 16:53 
Generalcompiler error Pin
daniellin27-Apr-04 23:52
daniellin27-Apr-04 23:52 
General...Compile error... Pin
Member 9699061-Apr-04 4:00
Member 9699061-Apr-04 4:00 
GeneralRe: ...Compile error... Pin
Chiew Heng Wah4-Apr-04 19:47
Chiew Heng Wah4-Apr-04 19:47 
The original schedule.cpp was compiled without a precompiled header (usually the file 'stdafx.h')

Since you are incorporating this in a new project you can do either:
1. Change the settings on the 'schedule.cpp' file.
- Under FileView, right click on 'schedule.cpp'
- Select Settings.../C++/Precompile Headers/Not using precompile headers

Alternatively:
2. Stick the following line at the top of 'schedule.cpp'

#include "stdafx.h"
Questionhow can i schedule function with parameters Pin
ariel biton8-Sep-03 5:47
ariel biton8-Sep-03 5:47 
AnswerRe: how can i schedule function with parameters Pin
Chiew Heng Wah8-Sep-03 18:10
Chiew Heng Wah8-Sep-03 18:10 
GeneralRe: how can i schedule function with parameters Pin
ariel biton8-Sep-03 20:25
ariel biton8-Sep-03 20:25 
QuestionHow to add this class to my appllication Pin
ariel biton11-Aug-03 20:56
ariel biton11-Aug-03 20:56 
AnswerRe: How to add this class to my appllication Pin
Chiew Heng Wah12-Aug-03 15:20
Chiew Heng Wah12-Aug-03 15:20 
Generalcompile error!! Pin
liuty20064-May-03 8:46
liuty20064-May-03 8:46 
GeneralRe: compile error!! Pin
jhaga4-May-03 10:07
professionaljhaga4-May-03 10:07 
GeneralRe: compile error!! - Casting Issue Pin
4-May-03 15:26
suss4-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.