在不使用任务调度程序的情况下调度 Windows 服务 [英] scheduling a windows service without using the task scheduler

查看:64
本文介绍了在不使用任务调度程序的情况下调度 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 服务,可以从特定文件夹中存档文件.我希望程序每天在特定时间运行.我可以使用任务调度程序来做到这一点.我想要做的是在不实际评估 Windows 任务调度程序 GUI 的情况下安排任务.也许是一个批处理脚本,即使系统处于睡眠状态,也可以安排程序每天运行,或者我可以做其他事情?有没有人知道如何实现这件事?

I have a windows service that archives files from a paticular folder. I want the program to run everday at a specific time. I can do that using the task scheduler. what I want to do is to schedule the task without actually assessing the windows task scheduler GUI. Maybe a batch script that schedules the program to run every day even when the system is on sleep or maybe something else i can do? does anyone have an idea of how this thing can be implemented?

推荐答案

解决方案非常基本.所以问题是,我们不是使用任务调度程序,而是在我们的代码中创建调度程序本身,因此将创建一个线程,该线程将始终检查我希望我的实际代码运行的时间以及当前时间是我希望方法运行的时间,它将触发主程序(在示例中,我要触发的方法名为 ArchiveFile).所以首先在 OnStart 我设置一个新的计时器并希望它每小时 24x7 触发.然后在 timer_elapse 中,我想检查当前时间是否是我希望我的方法执行的时间,如果为真,它将调用我想要执行的方法.(在本例中,我将时间设置为晚上 9 点或 21 点小时)

The solution is pretty basic . so the thing is that instead of using the task scheduler we are creating a scheduler itself in our code so there is a thread that will be created that will always be checking for the time that I want my actual code to run and once the current time is the time that I want the method to run it will trigger the main program(in the example the method I want to trigger is named ArchiveFile) . so first in the OnStart I am setting a new timer and want it to fire 24x7,every hour. then in the timer_elapse i want to check if the current time is the time I want my method to execute and if true it will call the method that I want to execute.(in this example I have set the time to be 9 pm or 21 hours)

    protected override void OnStart(string[] args)
    {
        timer = new System.Timers.Timer();                                        
        timer.Interval = 36000;                                                   // that fires every hour    
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);    //calling the elapse event when the timer elapses
        timer.AutoReset = true;                                                   // AutoReset is set to true as we want the timer to fire 24x7 so that the elapse event is executed only at the requried time  
        timer.Enabled = true;                                                                                                   

    }
    protected void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)       //everytime the elapse event occurs the TimeCheck method will be called 
    {
        TimeCheck();    
    }

    public void TimeCheck()    //method to check if the current time is the time we want the archiving to occur
    {

        var dt = DateTime.Now.Hour;
        if(dt==21)
        {
            Archivefile();
        }

    }

这篇关于在不使用任务调度程序的情况下调度 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆