Windows服务System.Timers.Timer的不点火 [英] Windows Service System.Timers.Timer not firing
问题描述
我有一个Windows服务写在C#这是为了执行任务,每隔几分钟。我使用的是 System.Timers.Timer的
此,但它并没有出现过火灾。我看的许多的不同岗位这里SO和其他地方,我没有看到有什么不对我的code。
I have a Windows service written in C# which is meant to perform a task every few minutes. I'm using a System.Timers.Timer
for this but it doesn't ever appear to fire. I've looked at many different posts here on SO and elsewhere and I'm not seeing what is wrong with my code.
下面是我的code,与为了清楚起见去非定时器相关的项目...
Here is my code, with non-timer related items removed for clarity...
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()
{
IntervalTimer.Enabled = false;
}
private void IntervalTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ // Do the thing that needs doing every few minutes...
DoWork();
}
}
}
我真的抓我的头在这一个。任何人都可以找出什么愚蠢的事情我越来越错了吗?
I'm really scratching my head over this one. Can anybody spot what silly thing I'm getting wrong?
编辑:
通过的建议,我加了 IntervalTimer.Enabled = TRUE;
在 IntervalTimer.Start();
在服务OnStart方法。这并不解决问题。
By suggestion, I added IntervalTimer.Enabled = true;
before IntervalTimer.Start();
in the service OnStart method. This doesn't resolve the issue.
我添加的文件跟踪记录到服务确认一些内部的,我肯定知道的Timer.Enabled值是的OnStart()结束的时候真的。
I've added file trace logging into the service to confirm some of the internals and I know for sure that the Timer.Enabled value is true by the time OnStart() is finished.
推荐答案
这是我的解决方法...
太多的时间寻找一个答案,这之后,我发现了各种各样的文章和博客在Windows服务中讨论的计时器。我见过的很多的关于这一点,他们都属于意见分为三大类,并降序排列频率:
After way too many hours searching for an answer to this, I discovered a wide variety of articles and blogs discussing timers in Windows services. I've seen a lot of opinions on this and they all fall into three categories and in descending order of frequency:
-
不要使用
System.Windows.Forms.Timer
,因为它不会工作。 (这仅是有道理的)
Don't use
System.Windows.Forms.Timer
because it won't work. (this only makes sense)
不要使用 System.Threading.Timer
,因为它不工作,使用 System.Timers.Timer的
代替。
Don't use System.Threading.Timer
because it doesn't work, use System.Timers.Timer
instead.
不要使用 System.Timers.Timer的
,因为它不工作,使用 System.Threading.Timer
代替。
Don't use System.Timers.Timer
because it doesn't work, use System.Threading.Timer
instead.
在此基础上,我试图2.这也似乎被微软推荐的,因为他们说的方法, System.Timers.Timer的
是的适用于服务器应用程序。
Based on this, I tried 2. This is also the approach that seems to be recommended by Microsoft since they say that System.Timers.Timer
is suited to "Server applications".
我所发现的是, System.Timers.Timer的
只是没有在我的Windows服务应用程序的工作。因此,我已经切换到 System.Threading.Timer
。这是一个滋扰,因为它需要一些重构,使其工作。
What I've found is that System.Timers.Timer
just doesn't work in my Windows Service application. Therefore I've switched over to System.Threading.Timer
. It's a nuisance since it requires some refactoring to make it work.
这是大约就是我的工作code貌似现在...
This is approximately what my working code looks like now...
namespace NovaNotificationService
{
public partial class NovaNotificationService : ServiceBase
{
private System.Threading.Timer IntervalTimer;
public NovaNotificationService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
TimeSpan tsInterval = new TimeSpan(0, 0, Properties.Settings.Default.PollingFreqInSec);
IntervalTimer = new System.Threading.Timer(
new System.Threading.TimerCallback(IntervalTimer_Elapsed)
, null, tsInterval, tsInterval);
}
protected override void OnStop()
{
IntervalTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
IntervalTimer.Dispose();
IntervalTimer = null;
}
private void IntervalTimer_Elapsed(object state)
{ // Do the thing that needs doing every few minutes...
// (Omitted for simplicity is sentinel logic to prevent re-entering
// DoWork() if the previous "tick" has for some reason not completed.)
DoWork();
}
}
}
我恨大夫,大夫,它伤害当我这样做......的解决方案,但是这就是我不得不求助于。一对桩下一个家伙,这个问题更多的意见了......
I hate the "Doctor, doctor, it hurts when I do this..." solution, but that's what I had to resort to. One more opinion on the pile for the next guy with this problem...
这篇关于Windows服务System.Timers.Timer的不点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!