Click here to Skip to main content
15,889,877 members
Articles / Operating Systems / Windows
Article

.NET Windows Service Scheduler

Rate me:
Please Sign up or sign in to vote.
3.29/5 (10 votes)
5 Oct 2006CPOL1 min read 92.6K   3K   35   13
Lets you schedule your Windows service to fire at appropriate times

Introduction

This program lets you schedule your Windows service to run at user configured intervals. It's inspired by this article by Andy Brummer.

The scheduler lets you schedule your Windows service code in one of the following three ways:

  1. Once a day
  2. Once a week
  3. Once a month

If you want to schedule multiple times, instantiate as many Scheduler objects you want.

It mails any exceptions to the user supplied email addresses. If the user chooses not to email, exceptions will be logged to the event log under "Service Scheduler".

Usage

Add a reference to the Scheduler.dll:

C#
using Sathish.ServiceScheduler;
Scheduler sch = new Scheduler("MyServiceName");
MailConfiguration mailConfig = new MailConfiguration(yourmail@gmail.com, 
    "admin@yourcompany.com", "Service Down", "localhost", "MyServiceName");

//If you don't do this, all your exceptions will be logged to the event log 
//under "Service Scheduler"
sch.MailComponent = mailConfig; 

//Subscribe to the schedulerFired event.
//I used EventHandler because it was straightforward. 
//You can write your own delegate signature.
sch.SchedulerFired += new EventHandler(YourServiceMethod); 

//Schedule weekly or monthly or daily
sch.ScheduleWeekly(DayOfWeek.Friday, "3:00 AM");

//sch.ScheduleMonthly(4, "6:20 PM"); //4 is 4th day of the month.
//sch.ScheduleDaily("4:00 AM");

The program checks if your mail infrastructure is in place. If not, it throws an exception.

And yes, it also takes care of 30/31/28/29 day months. So if you said...

C#
sch.ScheduleMonthly(31, "4:00 AM") 

... and the month had only 30 days, the program adjusts the difference and the scheduler fires on the last day (in this case 30th) of the month.

I haven't had the time to test it thoroughly. I will do it in the coming days and update any revisions. If any of you find anything amiss, please let me know at mailsash@gmail.com.

If you make enhancements, do send me a copy/link.

History

  • 5th October, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
Questionerror when scheduling monthly Pin
Member 107976634-Feb-19 23:30
Member 107976634-Feb-19 23:30 
QuestionTwo dates Pin
Nelson634013-Jan-17 10:10
Nelson634013-Jan-17 10:10 
GeneralMy vote of 4 Pin
sridevibathula11-Mar-12 20:00
sridevibathula11-Mar-12 20:00 
QuestionCan anybody help me? Pin
kalika_brandoa15-Nov-07 7:05
kalika_brandoa15-Nov-07 7:05 
AnswerRe: Can anybody help me? Pin
sashisme15-Nov-07 7:17
sashisme15-Nov-07 7:17 
GeneralRe: Can anybody help me? Pin
kalika_brandoa15-Nov-07 23:03
kalika_brandoa15-Nov-07 23:03 
GeneralRe: Can anybody help me? Pin
kalika_brandoa15-Nov-07 23:41
kalika_brandoa15-Nov-07 23:41 
GeneralRe: Can anybody help me? Pin
sridevibathula11-Mar-12 21:37
sridevibathula11-Mar-12 21:37 
GeneralRe: Can anybody help me? Pin
getvinay198819-Mar-14 19:59
getvinay198819-Mar-14 19:59 
Generalpossible limitation Pin
fidelitylife3-Jul-07 18:13
fidelitylife3-Jul-07 18:13 
QuestionHourly/other updates Pin
Dan B Atkinson10-Apr-07 23:40
Dan B Atkinson10-Apr-07 23:40 
Hey there!

I've been trying to mess around with the code so that I can create customised timing, and I was wondering if anyone had anything the same?

Basically, I have some services that run between 9-5 each day on the hour, every hour, and I have a couple of others which run every other hour between 9 and 5.

I was thinking of having an array of times (either ints or DateTimes) which you could supply to a new ScheduleOther.

I was also going to add something in for hourly updates which shouldn't be too bad, as you'd just get the hour now, and do a calculation from then to now and set that as the interval.

I know that this could easily be done by using timers, but I'd like to standardise my services so that they all use the same basic code (in this case, this great bit of code) to make updating it easier.

Anyway, if anyone has any hints/tips, I'd love to hear em.
<code>
TimeSpan nextRun = new TimeSpan();
DateTime now = new DateTime();
DateTime nextRunDate = new DateTime();

if (DateTime.Now.Hour >= 17 || DateTime.Now.Hour < 9)
{
//The next time would be 9 the next day
now = DateTime.Now.AddDays(1);
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 9, 0, 0);
}
else
{
//Time is between 9 and 17:00.
now = DateTime.Now;

if (now.Hour >= 9 && now.Hour < 10)
{
//nextRunDate hour is greater than 9, but less than 10.
//Next update is at 10.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 10, 0, 0);
}
else if (now.Hour >= 10 && now.Hour < 11)
{
//The hour is greater than 10, but less than 11.
//Next update is at 11.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 11, 0, 0);
}
else if (now.Hour >= 11 && now.Hour < 12)
{
//The hour is greater than 11, but less than 12.
//Next update is at 12.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 12, 0, 0);
}
else if (now.Hour >= 12 && now.Hour < 13)
{
//The hour is greater than 12, but less than 13.
//Next update is at 1.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 13, 0, 0);
}
else if (now.Hour >= 13 && now.Hour < 14)
{
//The hour is greater than 13, but less than 14.
//Next update is at 2.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 14, 0, 0);
}
else if (now.Hour >= 14 && now.Hour < 15)
{
//The hour is greater than 14, but less than 15.
//Next update is at 3.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 15, 0, 0);
}
else if (now.Hour >= 15 && now.Hour < 16)
{
//The hour is greater than 15, but less than 16.
//Next update is at 5.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 16, 0, 0);
}
else
{
//The hour is greater than 16, but less than 17.
//Next update is at 5.
nextRunDate = new DateTime(now.Year, now.Month, now.Day, 17, 0, 0);
}
}
nextRun = nextRunDate - DateTime.Now;
timer1.Interval = nextRun.TotalMilliseconds;
</code>

Any code optimisations are very welcome!
GeneralGood stuff Pin
Wirelessnz17-Oct-06 20:59
Wirelessnz17-Oct-06 20:59 
GeneralRe: Good stuff Pin
sashisme8-Oct-06 19:05
sashisme8-Oct-06 19:05 

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.