Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DispatcherTimer tick once

Tags:

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?

like image 669
JL. Avatar asked Oct 31 '10 14:10

JL.


2 Answers

private void dispatcherTimer_Tick(object sender, EventArgs e)         {             this.Visibility = System.Windows.Visibility.Visible;             (sender as DispatcherTimer).Stop();         } 
like image 158
JL. Avatar answered Sep 20 '22 22:09

JL.


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(); 
like image 23
Vlad Bezden Avatar answered Sep 20 '22 22:09

Vlad Bezden