Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the different between 'IsEnabled' and 'Start/Stop' of DispatcherTimer?

Tags:

c#

wpf

I think that IsEnabled = false/true is equally the same with Stop/Start method of class System.Windows.Threading.DispatcherTimer Am I right?

[EDIT] Start() : begin timer with a full interval countdown. IsEnabled = false : pause the timer, the interval countdown remains. IsEnabled = true : resume the timer & continue with the last used interval countdown. Stop() : stop the timer, will the interval countdown reset?

like image 586
Nam G VU Avatar asked Jul 02 '10 04:07

Nam G VU


People also ask

What is a dispatcher timer?

The DispatcherTimer is reevaluated at the top of every Dispatcher loop. Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations.

What is DispatcherTimer C#?

DispatcherTimer is the regular timer. It fires its Tick event on the UI thread, you can do anything you want with the UI. System. Timers. Timer is an asynchronous timer; its Elapsed event runs on a thread pool thread.


2 Answers

Considering that Start/Stop toggles the IsEnabled property, your assumption is close.

Start/Stop differs as the Interval is reset, where as just toggling the IsEnabled will not reset the Interval.

From MSDN:

Setting IsEnabled to false when the timer is started stops the timer.

Setting IsEnabled to true when the timer is stopped starts the timer.

Start sets IsEnabled to true.

Start resets the timer Interval.

EDIT: What I mean by the interval being reset is not the Interval property itself, but the background interval that determines how long until the next tick event is fired.

Eg. If you have an interval of 1000ms and you stop/disable it if with 250ms to run (it's run for 750ms), this is the result depending on how you start it again.

  • If you start it with Start(), then the passed interval will be reset back to 0 and it will be 1000ms before the Tick event is raised.
  • If you re-enable it with IsEnabled = true then it will continue from it's current location and the Tick event will be raised in 250ms.

I hope this clarifies it for you.

like image 117
Alastair Pitts Avatar answered Sep 19 '22 20:09

Alastair Pitts


Implementation of DispatcherTimer.IsEnabled

    public bool IsEnabled     {        get        {            return _isEnabled;        }        set        {            lock (_instanceLock)            {                if (!value && _isEnabled)                {                    Stop();                }                else                {                    if (!value || _isEnabled)                        return;                    Start();                }            }        }     } 
like image 33
Maxim Kapitonov Avatar answered Sep 19 '22 20:09

Maxim Kapitonov