5,693,062 members and growing! (17,795 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

.NET Windows Service Scheduler

By sashisme

Lets you schedule your Windows service to fire at appropriate times
Windows, .NET, Visual Studio, Dev

Posted: 5 Oct 2006
Updated: 5 Oct 2006
Views: 18,769
Bookmarked: 18 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.67 Rating: 2.38 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
2 votes, 40.0%
4
2 votes, 40.0%
5

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:

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...

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)

About the Author

sashisme



Location: India India

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralCan anybody help me?memberkalika_brandoa8:05 15 Nov '07  
GeneralRe: Can anybody help me?membersashisme8:17 15 Nov '07  
GeneralRe: Can anybody help me?memberkalika_brandoa0:03 16 Nov '07  
GeneralRe: Can anybody help me?memberkalika_brandoa0:41 16 Nov '07  
Generalpossible limitationmemberfidelitylife19:13 3 Jul '07  
QuestionHourly/other updatesmemberDan B Atkinson0:40 11 Apr '07  
GeneralGood stuffmemberWirelessnz121:59 7 Oct '06  
GeneralRe: Good stuffmembersashisme20:05 8 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Oct 2006
Editor: Deeksha Shenoy
Copyright 2006 by sashisme
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project