[WPF] DispatcherTimer automatically firing _Tick event, without me calling .Start()


I know the solution to this behaviour, but I'm just really curious why it behaves like that.

When I initialize my timer by using this constructor, it will automatically start the timer and the tmrScanning_Tick event gets fired after 5 seconds:

this.tmrScanning = new DispatcherTimer(TimeSpan.FromSeconds(5), DispatcherPriority.Normal, tmrScanning_Tick, this.Dispatcher); 

I know I could also use the following way instead, which would not start the timer, but I am curious why it is like that.

this.tmrScanning = new DispatcherTimer(); this.tmrScanning.Interval = TimeSpan.FromSeconds(5); this.tmrScanning.Tick += tmrScanning_Tick; 


See DispatcherTimer Class on msdn to see, that there are no other overloaded constructors for me to use, where I could define an interval.

Another workaround would be: calling the .Stop method after initializing the DispatcherTimer instance using the overloaded constructor.

by d0xxx via /r/csharp

Leave a Reply