我有一个用C#编写的Windows服务,用于每隔几分钟执行一次任务.我正在使用它System.Timers.Timer,但它似乎永远不会发射.我在SO和其他地方查看过很多不同的帖子,我没看到我的代码有什么问题.
这是我的代码,为清楚起见删除了与非计时器相关的项目...
namespace NovaNotificationService
{
public partial class NovaNotificationService : ServiceBase
{
private System.Timers.Timer IntervalTimer;
public NovaNotificationService()
{
InitializeComponent();
IntervalTimer = new System.Timers.Timer(60000); // Default in case app.config is silent.
IntervalTimer.Enabled = false;
IntervalTimer.Elapsed += new ElapsedEventHandler(this.IntervalTimer_Elapsed);
}
protected override void OnStart(string[] args)
{
// Set up the timer...
IntervalTimer.Enabled = false;
IntervalTimer.Interval = Properties.Settings.Default.PollingFreqInSec * 1000;
// Start the timer and wait for the next work to be released...
IntervalTimer.Start();
}
protected override void OnStop() …Run Code Online (Sandbox Code Playgroud)