我有一个 Windows 服务,它有一个从配置中读取的计时器间隔。
DipRedipServiceTimer_Elapsed 事件在服务首次运行 1000 毫秒后被调用。用这个方法编写的代码必须在运行这个定时器的下一个周期之前执行。但是,即使第一个周期尚未完成,我也看到 DipRedipServiceTimer_Elapsed 事件触发。这个结果是两个线程在同一段代码和一些非常可怕的问题上工作。我怎样才能防止这种情况发生?请建议。
partial class DIPREDIPServiceHost : ServiceBase
{
#region Private Fields
/// <summary>
/// Timer for polling job pool on timely basis
/// </summary>
private Timer DipRedipServiceTimer;
#endregion
public DIPREDIPServiceHost()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
DipRedipServiceTimer = new Timer();
DipRedipServiceTimer.Enabled = true;
DipRedipServiceTimer.Interval = 1000;
DipRedipServiceTimer.AutoReset = false;
DipRedipServiceTimer.Elapsed += new ElapsedEventHandler(DipRedipServiceTimer_Elapsed);
}
protected override void OnStop()
{
if (DipRedipServiceTimer != null)
{
DipRedipServiceTimer.Enabled = false;
DipRedipServiceTimer.Stop();
DipRedipServiceTimer = null;
}
}
#region "Timer Elapsed Event"
/// <summary>
/// Handles the Elapsed event of the timer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
void DipRedipServiceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
//disable timers as at a given time only one thread should process dip/redip.
DipRedipServiceTimer.Stop();
DipRedipServiceTimer.AutoReset = false;
DipRedipServiceTimer.Enabled = false;
try
{
IDipRedipController controller = new DipRedipController();
try
{
DipRedipConfiguration config = controller.GetDipRedipConfiguration();
// In case configuration has been retrieved, set timer defined.
if (config != null)
{
//set timer interval after reading from config file.
DipRedipServiceTimer.Interval = config.FileGenerationInterval * 60000;
controller.dipRedipConfiguration = config;
LoggingHelper.LogMessage(String.Format("Dip Service timer initialized at {0}", DateTime.UtcNow), Source.EDiscDIPREDIPService, LogCategory.Exception);
//Process Dip
bool dipSuccess = controller.ProcessDIP();
//Process Re-Dip
bool redipSuccess = controller.ProcessREDIP();
//Enable timers for next cycle
LoggingHelper.LogMessage(String.Format("Dip Service timer completed at {0}", DateTime.UtcNow), Source.EDiscDIPREDIPService, LogCategory.Exception);
}
// In case configuration is null, get the default timer defined in App.Config file.
else
{
int interval = 0;
int.TryParse(ConfigurationManager.AppSettings.Get("DefaultTimerValue"), out interval);
DipRedipServiceTimer.Interval = interval * 60000;
LoggingHelper.LogWarning("Configuration for Dip/Redip could not be fetched from database.", Source.FileImportService, LogCategory.Exception);
}
DipRedipServiceTimer.Enabled = true;
DipRedipServiceTimer.Start();
}
catch (FaultException ex)
{
LoggingHelper.LogException("Exception Occured in DipRedipServiceTimer_Elapsed method of Dip/Redip Window Service", ex, Source.EDiscDIPREDIPService);
}
}
catch (Exception ex)
{
LoggingHelper.LogException("Exception Occured in the DipRedip Service Host Window Service", ex, Source.EDiscDIPREDIPService);
}
}
#endregion
}