简体   繁体   English

如何每天在特定时间运行Windows服务

[英]How to Run a Windows Service at specific time each day

I have a windows service that i need to run at specific time of day.Suppose the time id 11:00 PM.At present i have code to run this service on everyday but how to add time variant into this i am not able to get that. 我有一个Windows服务,我需要在一天的特定时间运行。假设时间ID为11:00 PM。目前我有每天运行此服务的代码,但如何向其中添加时变我无法获得那。 Here is my code in c#.. 这是我在C#中的代码。

protected override void OnStart(string[] args)
    {
        timer = new Timer();
        timer.Interval = 1000 * 60 * 60 * 24;//set interval of one day 
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        start_timer();

    }

    static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        // Add your code here
        readDataFromAd();

    }
    private static void start_timer()
    {
        timer.Start();
    }

Please help me in defining the time also along with the interval.The time Should be 11:00 PM and the timer should execute the Method Everyday. 请帮助我定义时间以及时间间隔。时间应为11:00 PM,计时器应每天执行“方法”。

The best option is to use Quartz schedular in windows service. 最好的选择是在Windows服务中使用Quartz时间表。 Using quartz you can also schedule multiple jobs in a single service based on time for execution like daily on 5 AM , every hour, every minute, weekly etc. It is too flexible to use. 使用石英,您还可以基于时间安排单个服务中的多个作业以执行,例如每天凌晨5点,每小时,每分钟,每周一次等。使用起来太灵活了。

I would recommend changing your approach. 我建议更改您的方法。 Services are normally used for long running processes that are always running. 服务通常用于始终运行的长时间运行的进程。 For processes that run on a schedule, windows has a built in component called "Task Scheduler" that is designed for running applications on a schedule. 对于按计划运行的进程,Windows具有一个内置组件,称为“任务计划程序” ,用于按计划运行应用程序。

You can simply take your application service code and paste it into a windows console application and then schedule the resultant exe to run on whatever schedule you see fit by using the Windows Task Scheduler . 您可以简单地将应用程序服务代码粘贴到Windows控制台应用程序中,然后使用Windows Task Scheduler安排生成的exe以您认为合适的任何时间表运行。

Hope this helps. 希望这可以帮助。

Quartz is great, but if all you want to do is run your service once a day then the builtin Windows Task Scheduler is a good option too. Quartz很棒,但是如果您每天要做的只是一次服务,那么内置的Windows Task Scheduler也是不错的选择。

You would: 你会:

  1. Change your service to remove the timer/sleep and call readDataFromAd() from a thread started from OnStart() ( discussion on why a thread may be necessary here ) 更改您的服务以删除计时器/睡眠并从OnStart()启动的线程中调用readDataFromAd()( 在这里讨论为什么可能需要线程
  2. Create a task in the Task Scheduler that executes the following command at 11 PM: 在任务计划程序中创建一个任务,该任务在晚上11点执行以下命令:

    NET START Your-Service-Name NET START您的服务名称

Try this: 尝试这个:

 protected override void OnStart(string[] args)
        {

                _timer.Enabled = true;

                DateTime currentTime = DateTime.Now;
                int intervalToElapse = 0;
                DateTime scheduleTime = Convert.ToDateTime(ConfigurationSettings.AppSettings["TimeToRun"]);

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

                _timer = new System.Timers.Timer(intervalToElapse * 1000);
                _timer.AutoReset = true;
                _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
                _timer.Start();
        }

private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
           //do your thing
//set it to run on a 24-hour basis
     _timer.Interval = 60 * 60 * 24 * 1000;

}

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

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