Question

I have a timer and I want to know if its efficient or not to test if it's the first time the timer raise the elapsed event or not (I want change the Interval property after the first time the Elapsed event is raised)

        _timer = new Timer(1000);
        _timer.Elapsed += 
            (src, e) =>
            {
                FireTrigger(this, EventArgs.Empty);
                if (isfirstTimeElapsed) // necessary ??
                {
                    _timer.Interval = 5000
                    isfirstTimeElapsed = false;
                }
            };

In term of performances what's the best choice ? test or not ?

Was it helpful?

Solution

Its certainly not useless, it is self documenting code that means anybody reading it immediately understands that you want to change the interval on the first run. If you skipped the if and just set the interval every time it might not be obvious why you were doing this. As far as efficiency is concerned I doubt you'd notice a difference but of course if you are worried then perform some tests.

OTHER TIPS

You can use System.Threading.Timer, in which you can set when first event should be fired, and the other ones

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top