我创建了一个作为服务运行的应用。此服务启动2个计时器,一个每5分钟检查一次小配置更新,每天检查一次以获得更好的应用程序更新。 现在我的工作几乎完美无缺,除了一件事,计时器...... 当我启动服务时,5分钟计时器立即运行而不是等待5分钟。在此运行之后,服务结束。有人能给我一个暗示吗?
private void App_OnStartup(object sender, StartupEventArgs e)
{
ServiceBase.Run(new SuiteService());
}
服务:
private readonly DispatcherTimer _startTimer = new DispatcherTimer();
private readonly DispatcherTimer _shortTimer = new DispatcherTimer();
private readonly DispatcherTimer _longTimer = new DispatcherTimer();
protected override void OnStart(string[] args)
{
Thread newThread = new Thread(InitializeTimer);
newThread.Start();
}
private void InitializeTimer()
{
//// Timer 1
//_startTimer.Interval = new TimeSpan(0, 0, 5);
//_startTimer.Tick += CompleteProcedure();
//_startTimer.Start();
// Timer 2
_shortTimer.Interval = new TimeSpan(0, 5, 0);
_shortTimer.Tick += MeanwhileCheck;
_shortTimer.Start();
// Timer 3
int milliSeconds = CalculateInterval();
_longTimer.Interval = new TimeSpan(0, 0, 0, 0, milliSeconds);
_longTimer.Tick += CompleteProcedure();
_longTimer.Start();
}
在此之后我停止_longTimer一次,做一些事情并重新开始,但我从不停止_shortTimer。所以我的问题是: 为什么应用程序“结束”?定时器不应该保持活着吗?