Click here to Skip to main content
Email Password   helpLost your password?

Introduction

Task Scheduler is a class that schedules and automatically fires events at a time you specify. All important triggers are implemented: OnlyOneTime, Daily, Weekly and Monthly.

So why another Task Scheduler?

We need for one of our Projects a Scheduler that

  1. Can check a Date or show all Dates on which the trigger will fire.  (See the Demo, Button "Show List")
  2. We need to schedule Tasks not only Daily, Weekly,... we need a combination of them. f.e. every Wednesday (Weekly Trigger) plus every last day in a Month (Monthly Trigger)

Features

Settings

Field Description
StartDate Specifies the first Date on which the trigger will fire
EndDate Specifies the last Date on which the trigger will fire
TriggerTime Specifies the Time on which the trigger will fire
Enabled Enable / Disable the trigger
TriggerSettings Set the appropriate trigger-dates as described below

TriggerItem.TriggerSettings Overview:
TriggerSettings.jpg

To activate a specific Date just set the appropriate flag(s):

// Activate Sunday on weekly trigger.
triggerItem.TriggerSettings.Weekly.DaysOfWeek[(int)DayOfWeek.Sunday] = true;

// Activate last Friday in January and February
triggerItem.TriggerSettings.Monthly.Month[(int)TaskScheduler.MonthOfTheYeay.January] = 
    true; 
triggerItem.TriggerSettings.Monthly.Month[(int)TaskScheduler.MonthOfTheYeay.February] = 
    true; 
triggerItem.TriggerSettings.Monthly.DaysOfMonth[(int)TaskScheduler.DayOccurrence.Last] =
    true; 
triggerItem.TriggerSettings.Monthly.WeekDay[(int)DayOfWeek.Sunday] = true;

Using TaskScheduler and TriggerItems

  1. Create a new Instance of TaskScheduler.
    // Create the TaskScheduler
    TaskScheduler _taskScheduler = new TaskScheduler();
  2. Create a new Trigger-Item, set start and end date + trigger time and if you like a tag.
    TaskScheduler.TriggerItem triggerItem = new TaskScheduler.TriggerItem();
    triggerItem.Tag = textBoxlabelOneTimeOnlyTag.Text;
    triggerItem.StartDate = dateTimePickerStartDate.Value;
    triggerItem.EndDate = dateTimePickerEndDate.Value;
    triggerItem.TriggerTime = dateTimePickerTriggerTime.Value;
    // And the trigger-Event :)
    triggerItem.OnTrigger += new TaskScheduler.TriggerItem.OnTriggerEventHandler(
        triggerItem_OnTrigger);

    Settings for "OneTimeOnly"

    TriggerItem.TriggerSettings.OneTimeOnly.Active = checkBoxOneTimeOnlyActive.Checked;
    triggerItem.TriggerSettings.OneTimeOnly.Date = 
        dateTimePickerOneTimeOnlyDay.Value.Date;

    Settings for "Daily"

    triggerItem.TriggerSettings.Daily.Interval = (ushort)numericUpDownDaily.Value;

    Settings for "Weekly"

    for (byte day = 0; day < 7; day++) // Set the active Days
    triggerItem.TriggerSettings.Weekly.DaysOfWeek[day] = 
        checkedListBoxWeeklyDays.GetItemChecked(day);

    Settings for "Monthly"

    for (byte month = 0; month < 12; month++) // Set the active Months
        triggerItem.TriggerSettings.Monthly.Month[month] = 
        checkedListBoxMonthlyMonths.GetItemChecked(month);
    
    // Set the active Days (0..30 = Days, 31=last Day) for monthly trigger
    for (byte day = 0; day < 32; day++)
        triggerItem.TriggerSettings.Monthly.DaysOfMonth[day] = 
        checkedListBoxMonthlyDays.GetItemChecked(day);
    
    // Set the active weekNumber and DayOfWeek
    // f.e. the first monday, or the last friday...
    // 0..4: first, second, third, fourth or last week
    
    for (byte weekNumber = 0; weekNumber < 5; weekNumber++)     
        triggerItem.TriggerSettings.Monthly.WeekDay.WeekNumber[weekNumber] = 
        checkedListBoxMonthlyWeekNumber.GetItemChecked(weekNumber);
    for (byte day = 0; day < 7; day++)
        triggerItem.TriggerSettings.Monthly.WeekDay.DayOfWeek[day] = 
        checkedListBoxMonthlyWeekDay.GetItemChecked(day);
  3. Add the trigger to the Collection and enable the Scheduler
    triggerItem.Enabled = true; // Set the Item-Active - State
    _taskScheduler.AddTrigger(item); // Add the trigger to List
    _taskScheduler.Enabled = true; // Start the Scheduler

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO REPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNice work ..plz help for my struggle
skyramesh
0:03 23 Jan '10  
The timer_tick event does not work when i call CreateSchedulerItem function from thread function .

for ex:

myThreadfn()
{
.....
....
createscheduleritem()..
...
}
in this case the timer_tick event doesn't work even trigger timer start?.
plz tell me any body asap..
GeneralRe: Nice work ..plz help for my struggle
Lothar Perr
6:25 3 Feb '10  
You need a synchronized call inside the timer - event-handler...

for example:

textBoxEvents.Invoke(new MethodInvoker(delegate
{
textBoxEvents.AppendText(e.TriggerDate.ToString() + ": " + e.Item.Tag + ", next trigger: " + e.Item.GetNextTriggerTime().DayOfWeek.ToString() + ", " + e.Item.GetNextTriggerTime().ToString() + "\r\n");
UpdateTaskList();
}
));
GeneralReally Nice article...
Kushagra Tiwari
3:49 7 Sep '09  
It's a really gud article..My vote of 4 Smile
GeneralNice work
Xmen W.K.
15:42 7 Aug '09  
nice work mate...bookmarked 'n' have a 5, well done Thumbs Up



TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

-----------------------------------------------
128 bit encrypted signature, crack if you can

GeneralRe: Nice work
Lothar Perr
5:17 8 Aug '09  
Thank You! :D
GeneralRe: Nice work
Donsw
17:20 21 Aug '09  
I agree good work, well done.

cheers,
Donsw
My Recent Article : Backup of Data files - Full and Incremental

GeneralRe: Nice work
Lothar Perr
22:11 21 Aug '09  
Thank you! Big Grin
GeneralNo discription of TaskScheduler class
flow1965
9:14 5 Aug '09  
Sorry missing some description on the class itself. How to calculate event, etc.

I have build one for a differnet project and can imagine the effort on the class.

My hint: I would use the System.Timers.Timer instead of the Forms.Timer. Less side effects, more precisely. The Forms.Timer took me days for debugging Wink
GeneralRe: No discription of TaskScheduler class
Lothar Perr
12:45 5 Aug '09  
You are right, a System.Timers.Timer is better in most cases...
I used the Forms.Timer because you don't need to invoke within the Event-Handler...

Replace the Timer and use following Event-Handler:

void triggerItem_OnTrigger(object sender, TaskScheduler.OnTriggerEventArgs e)
{
textBoxEvents.Invoke(new MethodInvoker(delegate
{
textBoxEvents.AppendText(e.TriggerDate.ToString() + ": " + e.Item.Tag + ", next trigger: " + e.Item.GetNextTriggerTime().DayOfWeek.ToString() + ", " + e.Item.GetNextTriggerTime().ToString() + "\r\n");
UpdateTaskList();
}));
}
GeneralSuggestion [modified]
Hasan Hyder
8:09 4 Aug '09  
In http://www.codeproject.com/KB/miscctrl/ISCMS__.aspx project job schedule in "Work in Progress" between range '7/1/2009' to '8/1/2009' job schedule is displayed in the form of graphical bars. You can do so, to enhance your presentation.

modified on Tuesday, August 4, 2009 8:42 PM

GeneralRe: Suggestion
Lothar Perr
9:15 4 Aug '09  
Thank you, not a bad Idea!
Let's see if I find time for it Smile
GeneralMy vote of 2
Country Man
5:20 4 Aug '09  
the user need 2 clicks to check a choise in the check-lists

first click will select item and the second click check the choise
RantRe: My vote of 2
jgauffin
5:49 4 Aug '09  
If you give a two for such small issue, you are really screwed up. Mad
GeneralRe: My vote of 2
Lothar Perr
8:55 4 Aug '09  
@Country Man
1) That is the DEMO, not the class itself!
2) Please forward your Post to Microsoft. This control is a System.Windows.Forms.CheckedListBox and the behavior depends on it. Wink

@Sacha Barber
Big Grin tnx
GeneralMy vote of 5
Spiff Dog
11:11 4 Aug '09  
Great project. A decent scheduler is tough to pull off.

Plus, I wanted to counter that guy who gave a "2" for the goofy traits of a Microsoft control. Confused

-------------------------
Spiffdog Design
It's ok.. he doesn't bite...

GeneralRe: My vote of 2
Xmen W.K.
15:39 7 Aug '09  
*bleep* off Dead



TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

-----------------------------------------------
128 bit encrypted signature, crack if you can

GeneralWe have done one of these for work, and I know what this feels like
Sacha Barber
22:38 2 Aug '09  
its good, have a 5

Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: We have done one of these for work, and I know what this feels like
Lothar Perr
9:14 4 Aug '09  
Big Grin tnx
Generalnice
pimb2
8:02 28 Jul '09  
nice & clear Smile
GeneralRe: nice
Lothar Perr
8:27 28 Jul '09  
Thank You! :D


Last Updated 28 Jul 2009 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010