Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How might I schedule a C# Windows Service to perform a task daily?

I have a service written in C# (.NET 1.1) and want it to perform some cleanup actions at midnight every night. I have to keep all code contained within the service, so what's the easiest way to accomplish this? Use of Thread.Sleep() and checking for the time rolling over?

like image 248
ctrlalt3nd Avatar asked Feb 02 '09 15:02

ctrlalt3nd


People also ask

How early can you schedule ac section?

This is why it's important to wait until at least 39 weeks for a scheduled c-section. If your pregnancy is healthy, it's best to let labor begin on its own. If your provider talks to you about scheduling a c-section, ask if you can wait until at least 39 weeks to have your baby.

Can I schedule ac section at 38 weeks?

Kirkeby Hansen advises women seeking elective C-section to wait until the 39th week of their pregnancy. "A woman should make sure she is not having her C-section too early. She should put her foot down and not have it at 37 or 38 weeks just because this fits into the hospital's plan," she says.

Can I plan my own C-section?

The American College of Obstetricians and Gynecologists believes the answer is a resounding no. There is even a name for this category of C-sections: cesarean delivery on maternal request (CDMR), aka a C-section without any medical reasons for needing one.

Can I schedule ac section at 37 weeks?

Otherwise, although a baby is considered full-term after 37 weeks, most doctors' offices won't schedule a c-section until you have reached 39 weeks gestation. Babies develop at different rates, and some aren't ready to be born at the 37-week mark. Over the past decade, doctors have studied late preterm births in depth.


2 Answers

I wouldn't use Thread.Sleep(). Either use a scheduled task (as others have mentioned), or set up a timer inside your service, which fires periodically (every 10 minutes for example) and check if the date changed since the last run:

private Timer _timer; private DateTime _lastRun = DateTime.Now.AddDays(-1);  protected override void OnStart(string[] args) {     _timer = new Timer(10 * 60 * 1000); // every 10 minutes     _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);     _timer.Start();     //... }   private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {     // ignore the time, just compare the date     if (_lastRun.Date < DateTime.Now.Date)     {         // stop the timer while we are running the cleanup task         _timer.Stop();         //         // do cleanup stuff         //         _lastRun = DateTime.Now;         _timer.Start();     } } 
like image 97
M4N Avatar answered Oct 10 '22 22:10

M4N


Check out Quartz.NET. You can use it within a Windows service. It allows you to run a job based on a configured schedule, and it even supports a simple "cron job" syntax. I've had a lot of success with it.

Here's a quick example of its usage:

// Instantiate the Quartz.NET scheduler var schedulerFactory = new StdSchedulerFactory(); var scheduler = schedulerFactory.GetScheduler();  // Instantiate the JobDetail object passing in the type of your // custom job class. Your class merely needs to implement a simple // interface with a single method called "Execute". var job = new JobDetail("job1", "group1", typeof(MyJobClass));  // Instantiate a trigger using the basic cron syntax. // This tells it to run at 1AM every Monday - Friday. var trigger = new CronTrigger(     "trigger1", "group1", "job1", "group1", "0 0 1 ? * MON-FRI");  // Add the job to the scheduler scheduler.AddJob(job, true); scheduler.ScheduleJob(trigger); 
like image 43
jeremcc Avatar answered Oct 10 '22 22:10

jeremcc