I just bumped into this code and I don't understand it. Is there a reason to use this design instead of just re-running the elapsed code with AutoReset true?
private readonly Timer Timer = new Timer();
protected override void OnStart(string[] args)
{
Logger.InfoFormat("Starting {0}.", ServiceName);
try
{
// If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses.
Timer.AutoReset = false;
Timer.Elapsed += Timer_Elapsed;
Timer.Interval = Settings.Default.ScriptingStatusLifeTime;
Timer.Start();
}
catch (Exception exception)
{
Logger.ErrorFormat("An error has occurred while starting {0}.", ServiceName);
Logger.Error(exception);
throw;
}
}
/// <summary>
/// Whenever the Schedule Service time elapses - go to the ScriptingStatus table
/// and delete everything created earlier than 1 hour ago (by default, read from ScriptingStatusLifeTime)
/// </summary>
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
// ScriptingStatusLifeTime defaults to 60 minutes.
DateTime deleteUntil = DateTime.Now.AddMilliseconds(Settings.Default.ScriptingStatusLifeTime * -1);
Logger.InfoFormat("Clearing all ScriptingStatus entries with ControlDate before: {0}.", deleteUntil);
RemoteActivator.Create<RemoteScriptingStatus>().DeleteUntil(deleteUntil);
}
catch (Exception exception)
{
Logger.Error(exception);
}
finally
{
Timer.Start();
}
}
Furthermore, I'm looking for a memoryleak in this code.
I just read this post: If the autoreset is set to false, will my timer be disposed automatically? which seems to imply that my Timer object needs to be disposed of properly. I don't see any calls to Dispose in the current file. I'm wondering if this Timer_Elapsed event is also introducing a leak?
timer. Start(); when you call the Start method timer starts ticking next moment and it starts firing the Elapsed event based on the interval set, and it keep ticking/firing Elapsed event for the interval you set at Step3 untill you execute the timer. Stop method.
Timer AutoReset Gets or sets a value indicating whether the Timer should raise the Elapsed event each time the specified interval elapses or only after the first time it elapses.
As I understand it, by having AutoReset
to true, the timer event being fired can overlap where the time the event takes to execute goes beyond the timeout value.
For example, timeout of 10 seconds but a workload of a 1 minute.
However with AutoReset
as false then the timer event will only fire once. You can restart the timer in your event and the timer can continue.
In the example this means the timer can fire after 10 seconds but if the event takes longer than 10 seconds there's no overlap, it will just re-start after the work is completed.
This is pretty much how I do it and also how you have it in your example code.
Addendum: The above is only true if you don't set a sync object, this is because the elapsed event is raised on the thread pool. If you set a sync object then I'd expect locking to block the elapsed event so that only one event can fire at a time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With