I have some code where the timer EventHandler has this
void timer_Tick(object sender, EventArgs e)
{
try
{
timerRescan.Stop();
ScanForIeInstances()
}
catch (Exception ex)
{
log.Warn("Exception 3", ex);
}
finally
{
timerRescan.Start();
}
}
Naturally there is a race condition with an external entity who may want to Stop the timer down….if the timer is in process and using a thread and someone calls timerRescan.Stop, the timer thread will call Start starting the timer back up again. I am trying to replace this code. There are two methods in java I know and I would like to know how to do both in C#
- Run a task every 5 seconds where 5 seconds is the distance between tasks firing
- Run a task and AFTER it ends+5seconds run the task again
I would like to use #2 and always fire 5 seconds from the END of the last firing of the event. How do I do that and which timer do I use in C# for that?
This then allows me to have a recurring timer, call start once and have no race condition with the stop(I would rather not have to implement synchronization though I know I could do that as a last resort…would rather just keep the code clean like I can in java)
OR IF you know java, what I am simply looking for is the equivalent of
ScheduledExecutorService.scheduleAtFixedRate – start to start
ScheduledExecutorService.scheduleWithFixedDelay – end to start
A quick way to handle this is to have the external entity set a flag, and then check to see whether that flag has been set:
This doesn’t solve the problem if the external entity has a handle on the timer, but the timer should probably be private anyway.