C# - Repeating a method call using timers

Posted by Jeremy Rudd on Stack Overflow See other posts from Stack Overflow or by Jeremy Rudd
Published on 2010-03-23T23:55:36Z Indexed on 2010/03/24 0:13 UTC
Read the original article Hit count: 794

Filed under:
|
|
|

In a VSTO add-in I'm developing, I need to execute a method with a specific delay. The tricky part is that the method may take anywhere from 0.1 sec to 1 sec to execute. I'm currently using a System.Timers.Timer like this:

    private Timer tmrRecalc = new Timer();

    // tmrRecalc.Interval = 500 milliseconds

    private void tmrRecalc_Elapsed(object sender, System.Timers.ElapsedEventArgs e){

        // stop the timer, do the task
        tmrRecalc.Stop();           
        Calc.recalcAll();
        
        // restart the timer to repeat after 500 ms
        tmrRecalc.Start();
    }

Which basically starts, raises 1 elapse event after which it is stopped for the arbitrary length task is executed. But the UI thread seems to hang up for 3-5 seconds between each task.

Do Timers have a 'warm-up' time to start? Is that why it takes so long for its first (and last) elapse?

Which type of timer do I use instead?

© Stack Overflow or respective owner

Related posts about c#

Related posts about timing