I basically want a dispatcher timer object to only execute once.
So I have the basic code:
DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 4); dispatcherTimer.Start();
Then inside the click event:
private void dispatcherTimer_Tick(object sender, EventArgs e) { this.Visibility = System.Windows.Visibility.Visible; // Now stop timer execution.. or kill the timer object }
How can I stop the timer or kill off the object after this execution?
private void dispatcherTimer_Tick(object sender, EventArgs e) { this.Visibility = System.Windows.Visibility.Visible; (sender as DispatcherTimer).Stop(); }
Here is alternative code using lambda expression:
var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(4)}; timer.Tick += (sender, args) => { this.Visibility = System.Windows.Visibility.Visible; timer.Stop(); }; timer.Start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With