C# - Windows Service with awareness of System Time

扶醉桌前 提交于 2019-11-30 19:16:33

I think you can achieve it quite well with a Windows Service as you mentioned. In one of our productions systems we have a windows service (different core functionality as the requested) implemented in the way below that is running safely for almost three years now.

Basically the aim of the following code is that the service executes certain method each time the internal timer (myTimer) wakes-up.

Here below is a basic implementation. In this example your core functionality should be placed at the method EvalutateChangeConditions, that is supposed to be executed each 60 seconds. I would also provide a public method for your managing client(s) to know the current "working-mode".

public partial class MyService : ServiceBase
{
    private System.Threading.Thread myWorkingThread;
    private System.Timers.Timer myTimer = new System.Timers.Timer();

    // [...] Constructor, etc

    protected override void OnStart(string[] args)
    {
        // Do other initialization stuff...

        // Create the thread and tell it what is to be executed.
        myWorkingThread = new System.Threading.Thread(PrepareTask);

        // Start the thread.
        myWorkingThread.Start();
    }

    // Prepares the timer, sets the execution interval and starts it.
    private void PrepareTask()
    {
        // Set the appropiate handling method.
        myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);

        // Set the interval time in millis. E.g.: each 60 secs.
        myTimer.Interval = 60000;

        // Start the timer
        myTimer.Start();

        // Suspend the thread until it is finalised at the end of the life of this win-service.
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }

    void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        // Get the date and time and check it agains the previous variable value to know if
        // the time to change the "Mode" has come.
        // If does, do change the mode...
        EvalutateChangeConditions();
    }

    // Core method. Get the current time, and evaluate if it is time to change
    void EvalutateChangeConditions()
    {
        // Retrieve the config., might be from db? config file? and
        // set mode accordingly.
    }

    protected override void OnStop()
    {
        // Cleaning stuff...
    }
}
TheEvilPenguin

If there's no reason that Windows Task Scheduler won't work for you, I would recommend you use that.

If you don't want to use task scheduler, I would have a simple loop which checks for any upcoming events (lock/unlock sites) and execute any which are due. If no events are coming up, sleep for a long period (Thread.Sleep()).
I haven't looked into any side effects of long sleeps, but a sleep time of one minute shouldn't consume much in the way of resources. If it wasn't a service, I would probably put in a termination check, but I assume that the service isn't meant to terminate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!