Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason a Timer would have AutoReset false, but then re-start itself during its elapsed event?

Tags:

c#

timer

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?

like image 829
Sean Anderson Avatar asked Oct 01 '13 16:10

Sean Anderson


People also ask

How do I stop a timer elapsed event?

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.

What is timer AutoReset?

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.


1 Answers

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.

like image 194
Lloyd Avatar answered Oct 19 '22 20:10

Lloyd