c#每天在特定时间调用Windows服务

时间:2017-07-10 05:24:21

标签: c# windows-services

我创建了一个windows service,每天会在 12 PM 执行。但由于某些问题,我无法理解为什么在午夜 12 AM

这是我的代码:

private Timer _timer = null;
private DateTime _scheduleTime;
private static int FixHours = 12;//12 P.M.

protected override void OnStart(string[] args)
{
    try
    {
        _timer = new Timer();
        //check for time, if service started in morning before fix hours then service call should be on fixhours else next day
        if (DateTime.Now.TimeOfDay.Hours < FixHours)
        {
            var staticDateTime = DateTime.Now.Date;
            staticDateTime = staticDateTime.AddHours(FixHours).AddMinutes(0).AddSeconds(0);
            _timer.Interval = staticDateTime.Subtract(DateTime.Now).TotalMilliseconds;
            Log.Debug("Schedule Time:- " + staticDateTime.ToString());
        }
        else
        {
            // Schedule to run once a day at 12 P.M.
            _scheduleTime = DateTime.Today.AddDays(1).AddHours(FixHours);
            _timer.Interval = _scheduleTime.Subtract(DateTime.Now).TotalMilliseconds;
            Log.Debug("Schedule Time:- " + _scheduleTime.ToString());
            Log.Debug("Total Milisecond:- " + _timer.Interval.ToString());
        }
        _timer.Elapsed += Timer_Elapsed;
        _timer.Enabled = true;
    }
    catch (Exception ex)
    {
        Log.Error(ex);
    }
}
private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Log.Debug("Service Called:-" + e.SignalTime.ToString());            
    // 1. If tick for the first time, reset next run to every 24 hours
    double totalInterval = FixHours * 60 * 60 * 1000;
    if (_timer.Interval != totalInterval)
        _timer.Interval = totalInterval;
}

这里我将修复时间声明为 12 PM 。在OnStart方法中,我编写了用于识别何时调用服务的代码。如果我今天在 12 PM 之前启动服务,那么它将调用该功能,如果我在 12 PM 之后启动,则应该在明天 12 PM

但有时它会在午夜时分打电话。我不知道我做错了什么。

这是我的日志:

2017-07-05 11:10:42,096 [4] DEBUG Schedule Time:- 7/5/2017 12:00:00 PM
2017-07-05 12:00:00,326 [6] DEBUG Service Called:-7/5/2017 12:00:00 PM
2017-07-05 15:47:18,097 [4] DEBUG Schedule Time:- 7/6/2017 12:00:00 PM
2017-07-05 15:47:18,113 [4] DEBUG Total Milisecond:- 72761917.9899
2017-07-06 12:00:03,981 [6] DEBUG Service Called:-7/6/2017 12:00:03 PM
2017-07-07 00:00:05,745 [1441] DEBUG Service Called:-7/7/2017 12:00:05 AM
2017-07-07 12:00:07,873 [1860] DEBUG Service Called:-7/7/2017 12:00:07 PM
2017-07-08 00:00:09,906 [422] DEBUG Service Called:-7/8/2017 12:00:09 AM
2017-07-08 12:00:12,031 [1019] DEBUG Service Called:-7/8/2017 12:00:12 PM
2017-07-09 00:00:14,299 [2282] DEBUG Service Called:-7/9/2017 12:00:14 AM
2017-07-09 12:00:16,334 [843] DEBUG Service Called:-7/9/2017 12:00:16 PM
2017-07-10 00:00:18,279 [2972] DEBUG Service Called:-7/10/2017 12:00:18 AM

1 个答案:

答案 0 :(得分:1)

如果您想在下一个特定时间安排一个动作,您也可以像下面这样解决它。

var now = DateTime.Now;
var scheduledTime = new DateTime(now.Year, now.Month, now.Day, FixHours, 0, 0);
if (scheduledTime < now)
    scheduledTime = scheduledTime.AddDays(1);

var timeout = scheduledTime - now;

var timer = new Timer(timeout.TotalMilliseconds);
timer.Enabled = true;
timer.Elapsed += Timer_Elapsed;