简体   繁体   中英

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM