I want to disable a button -to prevent double click: On a tablet PC you push once, it clicked twice and this is the easiest hack that I know- for a short period but I noticed/debugged the interval might be too long in practice; 50 ms vs > 2 seconds.
There is in only one line starts the timer and one line stops it. Randomly the interval is 50 ms or much bigger. There is no CPU consume, I just click the button with mouse on my 4 core desktop PC.
What would be the reason?
DispatcherTimer timerTouchDelay = new DispatcherTimer();
protected override void OnMouseDown(MouseButtonEventArgs e)
{
//Init
if (timerTouchDelay.Interval.Milliseconds == 0)
{
timerTouchDelay.Tick += new EventHandler(timerTouchDelay_Tick);
timerTouchDelay.Interval = new TimeSpan(0, 0, 0, 0, 50); //ms
}
if(timerTouchDelay.IsEnabled)
return;
timerTouchDelay.Start();
HandleKeyDown();
base.OnMouseDown(e);
}
private void timerTouchDelay_Tick(object sender, EventArgs e)
{
timerTouchDelay.Stop();
}
To understand why this is the case, I would highly recommend the following article:
Comparing the Timer Classes in the .NET Framework Class Library
For reference,
DispatcherTimeris very similar toSystem.Windows.Forms.Timer, for which the author states “If you’re looking for a metronome, you’ve come to the wrong place”. This timer is not designed to ‘tick’ at exact intervals.