kat*_*tit 19 .net c# windows-services timer
我正在编写Windows服务,每隔几分钟就会处理一些"东西".
这是一些代码:
public Service()
{
this.InitializeComponent();
this.ServiceName = Name;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.eventLog.Source = Name;
// initialize timer
this.timer.Elapsed += this.TimerElapsed;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);
if (this.processor.PrepareToRun())
{
this.processor.Run();
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如果this.processor.Run()需要很长时间会发生什么,下一次TimerElapsed活动会被提出?它会跳过吗?完成后会等待并尽快运行吗?我应该考虑这些场景和代码吗?
我正在使用 System.Timers.Timer
编辑:
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);
try
{
this.timer.Stop();
if (this.processor.PrepareToRun())
{
this.processor.Run();
}
}
catch (Exception ex)
{
LoggingAndNotifications.LogAndNotify(ex);
}
finally
{
this.timer.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2
public Service()
{
this.InitializeComponent();
this.ServiceName = Name;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.eventLog.Source = Name;
// initialize timer
this.timer.AutoReset = false;
this.timer.Elapsed += this.TimerElapsed;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
eventLog.WriteEntry("Starting syncronization...", EventLogEntryType.Information);
try
{
if (this.processor.PrepareToRun())
{
this.processor.Run();
}
}
catch (Exception ex)
{
LoggingAndNotifications.LogAndNotify(ex);
throw;
}
finally
{
this.timer.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*nna 14
它会在另一个线程上再次调用它.
根据操作的性质,您可能需要:
| 归档时间: |
|
| 查看次数: |
6327 次 |
| 最近记录: |