|
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:
- Once a day
- Once a week
- 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");
sch.MailComponent = mailConfig;
sch.SchedulerFired += new EventHandler(YourServiceMethod);
sch.ScheduleWeekly(DayOfWeek.Friday, "3: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
| You must Sign In to use this message board. |
|
| | Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh) | FirstPrevNext |
|
 |
|
|
im using the scheldule dll and i'm having trouble in the implementation of it .
My code(after several tries) is like this:
###########################################################
protected override void OnStart(string[] args) { // TODO: Add code here to start your service. EnvioAutomatico();
// sch.SchedulerFired += new EventHandler(EnvioAutomatico);
}
protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. }
public void EnvioAutomatico() { Scheduler sch = new Scheduler("MyServiceName"); MailConfiguration mailConfig = new MailConfiguration("Carlos-A-Mendes@telecom.pt", "Carlos-A-Mendes@telecom.pt","Teste","144.64.192.97","MyServiceName");
sch.MailComponent = mailConfig;
//sch.SchedulerFired += new EventHandler(EnvioAutomatico); sch.ScheduleDaily("17:30 AM"); }
###########################################################
Can you give some sugestions???
Thanks in advance 
Carlos Mendes
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Your EnvioAutomatico method will have this body:
public void EnvioAutomatico() { Scheduler sch = new Scheduler("MyServiceName"); MailConfiguration mailConfig = new MailConfiguration("Carlos-A-Mendes@telecom.pt", "Carlos-A-Mendes@telecom.pt","Teste","144.64.192.97","MyServiceName");
sch.MailComponent = mailConfig;
//Changed!! sch.SchedulerFired += new EventHandler(MyWorkerMethod)
sch.ScheduleDaily("17:30 AM");
}
private void MyWorkerMethod(object sender, EventArgs e) { //Put the code that you want to execute at 5:30 PM here (You can't say 17:30 AM as you must use a 12 hour time with AM/PM. So your 17:30 will become 5:30 PM. }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Just another think:
I tried the code and then i had sucess in the rebuild of the project.
Then i picked the exe file and pass it to the folder v1.1.4322 to use the InstallUtil.exe and instal the service that i created. in the command prompt window i tried to install the application but it keeps giving this error: 
"System.BadImageFormatException"... the exe is invalid
Can you tell me what's wrong?
Thanks in advance
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It works great however the ticker.Start() has an issue when the interval is higher then Int32.Max. This can occur on ScheduleMonthly if the remaining days is greater than 24.8 days...
does anyone have suggestions...
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
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!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I wanted something simple that I could use - and could not get Andy Brummer's code to work on Visual Studio 2005 + Vista - and this was simpler and just what I needed to get the job done using Windows Service.
Well done!
I used Event handler too - though I did not use the email class.
[P.S. If anyone want to use the scheduler class from VB.Net, Just add the Schduler project to your solution (or add reference to the dll) and do the following: Dim sch As Sathish.Scheduler = New Sathish.Scheduler("JobsService") AddHandler sch.SchedulerFired, AddressOf YourServiceMethod sch.ScheduleDaily("1:00 AM")
]
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
When you have lots of services running on numerous servers for sometime, it's very easy to forget where's what. It's naive to believe that we'll check the status of the services (or the eventlog) everyday. That's why it's nice to have email notification if something screws up. But if you have just a couple of services, i guess it's OK.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|