C# - Windows服务与系统时间意识 [英] C# - Windows Service with awareness of System Time

查看:213
本文介绍了C# - Windows服务与系统时间意识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法(使用配置实用程序,我会提供)写一个窗口服务,将在用户指定的时间打开一个特定的功能开启或关闭。基本上,用户将指定特定的时间,该计算机将进入工作专用模式(阻断Facebook等分心网站),然后当那些时间上,PC将返回到正常模式。

I am thinking about writing a windows service that will turn a certain functionality on or off at times specified by the user (using a configuration utility I will provide). Basically, the user would specify certain times that the PC would go into "work-only" mode (blocking Facebook and other distracting sites), and then when those times are up the PC will return to normal mode.

我已经想出一些方法来创建工作只的模式,但是我挣扎是如何知道什么时候进入和退出的该模式。我真的不希望与线程和定时器的工作,如果我能避免它,因为这似乎是它会造成一个可怕的很多开销,所以我要寻找将是某种方式要么:

I have already come up with a few ways to create the "work-only" mode, but what I am struggling with is how to know when to go into and out of that mode. I don't really want to work with threads and timers if I can avoid it since that seems like it would create an awful lot of overhead, so what I am looking for would be some way to either:


  • 挂钩到Windows API,如果有某种timeChanged()事件的要检查

  • 使用某种预在规定的时间

  • 触发事件 - 内置库
  • ,我没有想到的是其他一些方法是优雅而精彩

有谁知道做到这一点的最好方法是什么?

Does anyone know the best way to do this?

推荐答案

我想你可以实现它很好用的 Windows服务你所说的。在我们的生产系统之一,我们有一个窗口服务(不同的核心功能所要求的),下面的方法是安全,为近三年来现在正在运行实施。

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.

下面的代码的基本宗旨是,服务执行一定的方法每次内部定时器( myTimer )唤醒后。

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

下面下方则是基本实现。在本实施例的核心功能,应放置在 EvalutateChangeConditions ,是应该各60秒后要执行的方法。我也为你管理客户端(S),以了解当前的工作模式提供的公共方法。

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

这篇关于C# - Windows服务与系统时间意识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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