Home > Back-end >  Windows service that will perform certain tasks at the desired time
Windows service that will perform certain tasks at the desired time

Time:10-13

I have a code that I want to run at certain times of the day. For example 08.00 AM and 06.10 PM. I want to write a windows service for this job. But should I set the tick property of the timer object that I will use to perform the operation one-on-one at this time to 1 second? Does it tire my computer to do a check every second, or is there another way ?

CodePudding user response:

If the service is the only way to go, you can set the timer tick to be duration until next timer.

DateTime nowTime = DateTime.Now;

DateTime scheduledTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 18, 10, 0, 0); //Specify your scheduled time HH,MM,SS 

DateTime scheduledTime2 = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, 8, 0, 0, 0);

DateTime nextTime = //*Logic to work out next fire* 

if (nowTime > nextTime)
{
  scheduledTime = scheduledTime.AddDays(1);
}
 
double tickTime = (double)(nextTime - DateTime.Now).TotalMilliseconds;
timer = new Timer(tickTime);
  • Related