我有一个带有计时器 (System.Timers.Timer
) 的 .NET Windows 服务 (.NET 3.5)。 OnElapsed
方法如下所示:
private void OnTimerElapsed(object source, ElapsedEventArgs e)
{
lock (this)
{
timer.Stop();
//process some stuff here..
ProcessStuff();
timer.Interval = GetTimerInterval();
timer.Start();
}
}
它工作正常,直到它神秘地停止工作。这种情况每 x 天发生一次,尽管该服务的状态为已启动,但它不会启动 ProcessStuff
() 方法。我使用 log4net
,但那里或 Windows 事件日志中没有任何记录。 ProcessStuff
() 生成多个线程来完成一些工作。
我该如何解决?
最佳答案
有没有可能ProcessStuff()
或者GetTimerInterval()
抛出异常,导致timer.Start()
没有执行?
然后你应该把那部分包装在 try..catch
和/或添加一些日志记录,例如:
timer.Stop();
try
{
//process some stuff here..
ProcessStuff();
}
catch (Exception ex)
{
// log the exception?
}
timer.Interval = GetTimerInterval();
timer.Start();
关于.net - Windows 服务已启动但不执行任何操作 - .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2344966/