5

I found this answer and I try to do follow that, but It didn't bring me any work while I start my service. One thing that I couldn't understand is :`_timer = new Timer(10 * 60 * 1000); // every 10 minutes

I want to perform my service daily at 10:00PM, how could I do that?

Community
  • 1
  • 1
Nothing
  • 2,644
  • 11
  • 64
  • 115
  • Depending on what you're trying to run, it might be better to just build it as a console application and have the Windows Task Scheduler run it whenever you want. – 3Dave Aug 12 '12 at 01:02
  • @Nothing can you accept `Adil` answer? which is working fine and nice solution too. :) Since this question is under un-answered category. +1 your question and `Adil` answer. Helpful :)))) ., – RajeshKdev Apr 10 '14 at 09:21

4 Answers4

8

You can find out the difference between current time and your desired schedule time and make the elapse interval accordingly. This way the event of timer will fire once at the sheduled time. Set the interval of time to 24 hours when it is elapsed.

private System.Timers.Timer _timer;    

private void SetTimer()
{
    DateTime currentTime = DateTime.Now;
    int intervalToElapse = 0;
    DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 10,0,0);

    if (currentTime <= scheduleTime)
        intervalToElapse = (int)scheduleTime.Subtract(currentTime).TotalSeconds;
    else
        intervalToElapse = (int)scheduleTime.AddDays(1).Subtract(currentTime).TotalSeconds;

    _timer = new System.Timers.Timer(intervalToElapse);

    _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
    _timer.Start();
}

void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
       //do your work
       _timer.Interval = (24 * 60 * 60 * 1000);
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    Do I have to use `SetTimer` method in `OnStart` ? – Nothing Aug 12 '12 at 01:36
  • 2
    You can call it where ever you like, but calling on OnStart will make sure it will not missing the schedule task. e.g your service started on 9:58 then it must be called so that the event of timer elapse at 10 – Adil Aug 12 '12 at 01:48
  • TotalSeconds should be replaced with TotalMilliseconds because the Timer accepts a value as milliseconds and not as seconds. – Andrei Petrut May 17 '19 at 13:39
4

Instead of rolling your own timer you can use Quartz.NET to host the service. It works well and has some additional features like catch up (if you need it) as well as the ability to farm the service.

http://quartznet.sourceforge.net/

AdamSane
  • 2,831
  • 1
  • 24
  • 28
  • hmm - this sounds good but I took a look at the page and it didn't have much info, do you have a code example? **Edit** - [found it](http://quartznet.sourceforge.net/tutorial/lesson_1.html) – Jeremy Thompson Aug 12 '12 at 01:11
3

Windows has a built in task scheduler that is much better to use for any scheduling needs. There are a lot of things that can go wrong when working with scheduling tasks such as a system reboot, day light savings.

Can refer to a blog by Ayende about some of the issues wrong with making your own scheduler. Rotten scheduling: Don’t roll your own

aledpardo
  • 761
  • 9
  • 19
Josh
  • 1,058
  • 9
  • 27
1

Even though the Timer is polling every ten minutes in the answer you reference - its only processing once a day:

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // ignore the time, just compare the date
    if (_lastRun.Date < DateTime.Now.Date)
    {

To make it happen at 10:00AM add another condition, eg:

_lastRun.Date < DateTime.Now.Date && DateTime.Now.Hour = 10

The ten minutes is the time used as a polling frequency.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • So could you tell me, what does that 10 minutes mean? thanks. – Nothing Aug 12 '12 at 01:01
  • Please see my edit. If you want the code to run it on a scheduled task simply have an exe that when you start with a command line parameter it runs the process, but if you were to double click the exe it would show you the instructions on how to run it unattended - this way people dont accidentally kick of a process when they are wondering what the program does. – Jeremy Thompson Aug 12 '12 at 01:07