CZA
CZA

Reputation: 35

How do I create a timer for WPF

I have a button_click event and a button_PreviewMouseLeftButtonDown event. I want to set a timer for my button_PreviewMouseLeftButtonDown event. If the user's mouse is down for more than 1 second, then my code executes the button_PreviewMouseLeftButtonDown event. How can I accomplish this?

Upvotes: 0

Views: 3334

Answers (1)

Smaiil
Smaiil

Reputation: 157

You should use a DispatcherTimer:

using System.Windows.Threading;
...

DispatcherTimer timer = new DispatcherTimer();
timer.Tick += TimerTick;
timer.Interval = TimeSpan.FromSeconds(1);
timer.Start();
...

private void TimerTick(object sender, EventArgs e)
{
    // Put some code here
}

Upvotes: 4

Related Questions