Win32 Schedule Class






2.92/5 (10 votes)
May 1, 2003
2 min read

91047

2186
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