全部博文(584)
分类: WINDOWS
2011-03-31 09:48:01
Microsoft developed a full-featured scheduler for IE 4, IE 5, Win 98, and NT 5. From looking at all the task options available (three property pages, plus an "advanced" dialog) you could imagine that programming the scheduler is quite a chore. And you'd be right. I developed the CScheduledTask class to help you create very simple tasks and understand the basics of using the scheduler.
There is plenty of room for improvement in this class. This was done purposely on my part -- making a class to implement all the features of the scheduler would have amounted to rewriting the scheduler UI, which I really didn't want to do (for free ;)). As it currently stands, CScheduledTask can create and delete events from the scheduler. Tasks can have simple schedules, namely one time only, daily, weekly, or monthly.
CScheduledTask was written with MSVC 6.0 on Win 98. It should work OK in Unicode, although I haven't tested it. I do use a couple of 6.0-specific functions, such as CTime::GetAsSystemTime() and CString::Delete(), so in order to build with MSVC 5.0, you'd need to convert those sections back to use MFC 4.2 functions. You would also need the INetSDK or Platform SDK installed, because you need to link with MSTASK.LIB for the scheduler GUIDs and #include MSTASK.H for the interfaces.
Please note that this class does not interface with the AT service, the default scheduler on NT 4 and earlier.
Creating a taskSo, you want to schedule a program, eh? Here is the information you'll need to provide to CScheduledTask:
There's a little catch when creating a weekly repeating task: if you want the task to repeat on Mondays, the starting date must be a Monday. Similarly, for monthly tasks, if you want the task to run on the 20th, the starting date must be a 20th.
The CScheduledTask member functions for doing all this are listed below:
Note that there are two functions for setting the starting time, and two for the ending date. The two overloads let you pass in a CTime or SYSTEMTIME, depending on whatever format you use in your code.
ETaskFrequency is an enumeration that has the following values:
Once you've set all the relevant info, call the SaveTask() function:
The return value is S_OK if the save was successful. If you call this function before setting a required parameter (i.e., task name, start date/time, or frequency), the return value is E_FAIL. If a scheduler COM method fails, the HRESULT returned is the error returned by the last COM method that was called. In the debug version, CScheduledTask will display a trace message listing which method failed.
The bFailIfExists parameter determines if SaveTask() will bail out if a task with the given name already exists. The default behavior is to replace an existing task.
Deleting a taskDeleting a task is quite easy - just call the DeleteTask function:
Note that this is a static function, and it works independently of everything else in the class. This function also returns an HRESULT, similar to SaveTask().
Other functionsIf you have the need to clear out the contents of a CScheduledTask object, call the Reset function:
I have also included accessor functions to return the various task parameters. They are:
These functions are there for the sake of completeness, they were easy to write, and they also provide a starting point for future enhancements, such as the ability to enumerate through existing tasks in the scheduler.
GetStartDateTime() and GetEndDate() return a BOOL indicating if the corresponding date/time has been set. They return TRUE if the date/time is set, or FALSE if not. GetFrequency() returns the special value freqUnset if no frequency has been set. The remaining functions return an empty string if the corresponding item has not been set. Note that there is no GetPassword() function, because the Task Scheduler API doesn't provide a way to retrieve the password.
UpdatesApril 18, 1999: Version 1.1 has these changes:
November 27, 1999: Version 1.2 has these changes:
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
Software Developer (Senior)
VMware United States Member |
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from
with a math degree in 1994, and immediately landed a job as a QA
engineer at Symantec, working on the Norton AntiVirus team. He pretty
much taught himself Windows and MFC programming, and in 1999 he designed
and coded a new interface for Norton AntiVirus 2000. Mike has been a a developer at and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at . He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese. Mike was a from 2005 to 2009. |