schedular in windows service
I had written one windows service in C#.NET which fires on every miniutes... I want to fire a mail to my manager in every month on say xyz date.... this task shld repeted on every month on same date... So can any one tell me h开发者_StackOverflow中文版ow to do this..? I mean to say the block of code which i will write shld get executed on same date of every month...
This will give you a TimeSpan of the number of days/hours/minutes/etc. until the first day of the next month:
TimeSpan ts = DateTime.Now.AddDays(
(DateTime.Now.Day - 1) * -1).AddMonths(1) - DateTime.Now;
Use DateTime.Today
for midnight of the first day of next month.
You can use that to set the interval for a timer.
this looks like the perfect usage for quartz.net
var schedulerFactory = new StdSchedulerFactory();
var scheduler = schedulerFactory.GetScheduler();
scheduler.Start();
var jobDetail = new JobDetail("myJob", null, typeof (MyFooImplementation)); // MyFooImplementation needs to impelement IJob
var trigger = TriggerUtils.MakeMonthlyTrigger("myJobTrigger", 1, 0, 0);
scheduler.ScheduleJob(jobDetail, trigger);
more infos on .MakeMonthlyTrigger
can be found here
otherwise i would got for the windows task scheduler ... the good old-fashioned way :)
精彩评论