定时运行函数的Windows服务

26

我想要启动一个Windows服务,在特定时间每天运行一个函数。

我应该考虑哪种方法来实现这个功能?是使用计时器还是线程?

7个回答

80

(1) 首次启动时,将 _timer.Interval 设置为服务启动时间和计划时间之间的毫秒数。本示例将计划时间设置为早上 7:00,即 _scheduleTime = DateTime.Today.AddDays(1).AddHours(7);

(2) 在 Timer_Elapsed 事件中,如果当前间隔不是 24 小时,则将 _timer.Interval 重置为 24 小时(以毫秒为单位)。

System.Timers.Timer _timer;
DateTime _scheduleTime; 

public WinService()
{
    InitializeComponent();
    _timer = new System.Timers.Timer();
    _scheduleTime = DateTime.Today.AddDays(1).AddHours(7); // Schedule to run once a day at 7:00 a.m.
}

protected override void OnStart(string[] args)
{           
    // For first time, set amount of seconds between current time and schedule time
    _timer.Enabled = true;
    _timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;                                          
    _timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
}

protected void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // 1. Process Schedule Task
    // ----------------------------------
    // Add code to Process your task here
    // ----------------------------------


    // 2. If tick for the first time, reset next run to every 24 hours
    if (_timer.Interval != 24 * 60 * 60 * 1000)
    {
        _timer.Interval = 24 * 60 * 60 * 1000;
    }  
}

编辑:

有时人们希望将服务的启动时间安排在第0天,而不是明天,因此他们会更改DateTime.Today.AddDays(0)。如果他们这样做,并设置过去的时间,则会设置间隔时出现负数错误。

//Test if its a time in the past and protect setting _timer.Interval with a negative number which causes an error.
double tillNextInterval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;
if (tillNextInterval < 0) tillNextInterval += new TimeSpan(24, 0, 0).TotalSeconds * 1000;
_timer.Interval = tillNextInterval;

1
这个计时器不是每分钟运行一次,而是每天只运行一次。 - Settapon H
4
看起来您提供了一个不错的代码示例。为了具体回答用户的问题,最好在代码周围添加一些解释。 - RacerNerd
6
如果您希望在特定时间运行它,可以考虑将其制作为普通控制台应用程序,并使用Windows任务计划程序来运行,而不是将其作为一项服务。 - MarceloBarbosa
2
我认为如果任务执行时间很长,下一次它不会准确地在7点运行。它会继续增长。 - Mukus
而不是“if (_timer.Interval != 24 * 60 * 60 * 1000) { _timer.Interval = 24 * 60 * 60 * 1000; }”,最好做的是_timer.Interval = (_scheduleTime - DateTime.Now).TotalMilliseconds,这样,无论函数花费多长时间,只要少于24小时即可。 - C sharper
显示剩余2条评论

8

不错的答案(我使用了您的代码),但是这一行存在一个问题:

_timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalSeconds * 1000;

如果DateTime.now晚于scheduleTime,那么你将会变为负数,并且在分配给timer.Interval时会生成异常。
我使用的是:
if (DateTime.now > scheduleTime)
    scheduleTime = scheduleTime.AddHours(24);

然后进行减法操作。

在这种情况下,你应该使用while (DateTime.now > scheduleTime)的语句块: { scheduleTime = scheduleTime.AddHours(24);} 如果时间间隔超过1天,这样做会更好...它可以解决可能发生的任何奇怪问题。 - fireshark519

8
你确定需要一个每天只运行一次的服务吗?也许使用Windows任务计划会是更好的解决方案?

4

有没有使用Quartz.net的完整示例?是在Windows服务中的_AppDomain还是控制台应用程序?_ - Kiquenet

0
private static double scheduledHour = 10;
private static DateTime scheduledTime;

public WinService()
{
     scheduledTime = DateTime.Today.AddHours(scheduledHour);//setting 10 am of today as scheduled time- service start date
}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
      DateTime now = DateTime.Now;
      if (scheduledTime < DateTime.Now)
      {
         TimeSpan span = now - DateTime.Now;
         scheduledTime = scheduledTime.AddMilliseconds(span.Milliseconds).AddDays(1);// this will set scheduled time to 10 am of next day while correcting the milliseconds
         //do the scheduled task here
      }  
}

0

你可以使用线程和事件来完成它,不需要定时器。

using System;
using System.ServiceProcess;
using System.Threading;

partial class Service : ServiceBase
{
    Thread Thread;

    readonly AutoResetEvent StopEvent;

    public Service()
    {
        InitializeComponent();

        StopEvent = new AutoResetEvent(initialState: false);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            StopEvent.Dispose();

            components?.Dispose();
        }

        base.Dispose(disposing);
    }

    protected override void OnStart(string[] args)
    {
        Thread = new Thread(ThreadStart);

        Thread.Start(TimeSpan.Parse(args[0]));
    }

    protected override void OnStop()
    {
        if (!StopEvent.Set())
            Environment.FailFast("failed setting stop event");

        Thread.Join();
    }

    void ThreadStart(object parameter)
    {
        while (!StopEvent.WaitOne(Timeout(timeOfDay: (TimeSpan)parameter)))
        {
            // do work here...
        }
    }

    static TimeSpan Timeout(TimeSpan timeOfDay)
    {
        var timeout = timeOfDay - DateTime.Now.TimeOfDay;

        if (timeout < TimeSpan.Zero)
            timeout += TimeSpan.FromDays(1);

        return timeout;
    }
}

0

如果只是一天中的一次,为什么不使用任务计划程序呢? 当您想要在一分钟内运行多次任务时,Windows服务非常有用。因此,如果您想在特定时间运行程序,最好使用任务计划程序,并在一天中的特定时间设置任务计划程序事件。 我经常使用任务计划程序来完成很多事情,效果非常完美。 您可以在任务计划程序中设置程序路径和运行间隔时间。 即使您想在一天中每5分钟运行一次程序,仍然可以使用任务计划程序,这是更好的方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接