65.9K
CodeProject 正在变化。 阅读更多。
Home

.NET Windows 服务计划程序

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.29/5 (10投票s)

2006年10月5日

CPOL

1分钟阅读

viewsIcon

93648

downloadIcon

2970

允许您安排 Windows 服务在适当的时间触发

引言

该程序允许您以用户配置的间隔来安排 Windows 服务运行。它受到 Andy Brummer 的这篇文章的启发。

该计划程序允许您以以下三种方式之一安排 Windows 服务代码

  1. 每天一次
  2. 每周一次
  3. 每月一次

如果您想安排多次运行,请实例化您需要的 Scheduler 对象数量。

它会将任何异常发送到用户提供的电子邮件地址。如果用户选择不发送电子邮件,异常将被记录到事件日志中,类别为“服务计划程序”。

用法

添加对 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");

该程序会检查您的邮件基础设施是否就绪。如果未就绪,它将抛出异常。

是的,它还会处理 30/31/28/29 天的月份。所以,如果您指定…

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

…并且该月只有 30 天,程序会调整差异,并且计划程序将在该月的最后一天(在本例中为 30)触发。

我还没有时间彻底测试它。我将在未来几天进行测试并更新任何修订。如果您发现任何问题,请通过 mailsash@gmail.com 与我联系。

如果您进行了改进,请发送给我一份副本/链接。

历史

  • 2006 年 10 月 5 日:初始发布
© . All rights reserved.