Schedule a C # Windows service to perform a task daily

I found this answer , and I try to follow this, but it has not brought me any work while I begin my service. The only thing I could not understand was: `_timer = new timer (10 * 60 * 1000); // every 10 minutes

I want to do my service daily at 10:00 pm, how can I do this?

+5
source share
4 answers

You can find out differencebetween current timeboth yours desired schedule timeand the appropriate interval. Therefore, a timer event will fire onceat the specified time. Set the time interval to 24 hours when it has expired.

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);
}
+7
source

, , Quartz.NET . , "" ( ), .

http://quartznet.sourceforge.net/

+4

Windows , . , , , , .

, . :

+3

, 10 , - :

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

10:00, , :

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

Ten minutes is the time used as the polling rate.

+1
source

All Articles