Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timer in an event handler?

Am currently building an app and I want the timer to start only when I click a particular button.

So is there anyway to start timer once a button is clicked? (I don't want the timer to start as soon as the page loads)

like image 588
Balachander Ramachandran Avatar asked Aug 17 '26 15:08

Balachander Ramachandran


1 Answers

Check this post.

//Inside Page Load 
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
dt.Tick += new EventHandler(dt_Tick);

Tick Event Handler for your timer

void dt_Tick(object sender, EventArgs e)
{
    // Do Stuff here.
}

Now on your button click event handler you will do

dt.Start();

Hope this helps.

like image 176
Amar Palsapure Avatar answered Aug 20 '26 04:08

Amar Palsapure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!