简体   繁体   English

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

[英]c# call windows service every day at specific time

I have created one windows service which will execute every day at 12 PM But due some issue I am not getting why it is called at Mid night 12 AM 我创建了一个windows service ,该windows service每天每天12 PM执行,但是由于某些问题,我不明白为什么在午夜12 AM调用它

Here is my code: 这是我的代码:

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;
}

Here I declare fix hours to 12 PM . 在这里,我宣布固定工作时间为12 PM In the OnStart method I have written code which will use to identify when to call service. OnStart方法中,我编写了用于标识何时调用服务的代码。 If I start the service today before 12 PM then it will call the function, and if I start after 12 PM then it should call tomorrow at 12 PM . 如果我今天下午12点之前启动服务,则它将调用该函数,如果我下午12点之后启动,则它将在明天12 PM调用。

But sometime it is calling at midnight. 但是有时它在午夜打电话。 I don't know where I am doing wrong. 我不知道我在哪里做错了。

This is my log: 这是我的日志:

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

If you wanted to schedule an action allways on the next specific time you could also solve it like the following. 如果您想在下一个特定时间始终安排一项操作,则也可以像下面这样解决。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM