我认为您可以使用您提到的Windows 服务很好地实现它。在我们的一个生产系统中,我们有一个以以下方式实现的 Windows 服务(根据要求提供不同的核心功能),该服务已经安全运行了近三年。
基本上,以下代码的目的是每次内部计时器(myTimer
)唤醒时服务执行特定的方法。
下面是一个基本的实现。在这个例子中,你的核心功能应该放在方法EvalutateChangeConditions
中,它应该每 60 秒执行一次。我还将为您的管理客户提供一种公共方法,以了解当前的“工作模式”。
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...
}
}