[英]How can I use threading to 'tick' a timer to be accessed by other threads?
When the user does something (touch on a StackPanel, in this case), I need to begin a timer of some sort (probably DispatcherTimer as I'm working in WPF) and if another touch happens again within a certain amount of time then I'll call a method. 当用户执行某项操作(在这种情况下,在StackPanel上进行触摸)时,我需要启动某种计时器(可能是在WPF中工作时为DispatcherTimer),并且如果在一定时间内又发生另一次触摸,那么我会调用一个方法。 As you can probably guess - this is to implement a double-tap functionality.
您可能会猜到-这是实现双击功能。
I'm assuming the best way to achieve this is through using threads (ie a child thread to increment a timespan which can be checked by the Main thread any time the StackPanel is touched again?) 我假设实现此目标的最佳方法是使用线程(例如,子线程增加时间跨度,只要再次触摸StackPanel即可由主线程检查该时间跨度?)
Thanks, 谢谢,
Dan 担
You do not need to start another thread to do this. 您不需要启动另一个线程来执行此操作。
Just take a timestamp of when the first tap happened and use this. 只需记录一下第一次点击发生的时间,然后使用即可。 You can then calculate the timespan by subtracting this time from the current time:
然后,您可以通过从当前时间中减去该时间来计算时间跨度:
private DateTime _lastTap;
public void TapHandler()
{
DateTime now = DateTime.UtcNow;
TimeSpan span = now - lastTap;
_lastTap = now;
if (span < TimeSpan.FromSeconds(1)) {...}
}
Alternatively, as suggested by @DannyVarod, you can use a Stopwatch
to achieve the same result (but with more accurate timing): 或者,按照@DannyVarod的建议,您可以使用
Stopwatch
获得相同的结果(但计时更准确):
private Stopwatch _stopwatch = new Stopwatch();
public void TapHandler()
{
TimeSpan elapsed = _stopwatch.Elapsed;
_stopwatch.Restart();
if (elapsed < TimeSpan.FromSeconds(1)) {...}
}
The best way is to stick with DispatcherTimer
as you first pointed out, as this will ensure you don't need to do any thread marshalling on tick. 最好的方法是像您最初指出的那样坚持使用
DispatcherTimer
,因为这将确保您无需在滴答声中进行任何线程编组。 If you explictly need accuracy and/or background threads, please see the System.Timers.Timer
class and System.Threading.Timer
class. 如果您明确需要精度和/或后台线程,请参见
System.Timers.Timer
类和System.Threading.Timer
类。
A code example which allows differentiation between Single and Double clicks can be found on MSDN (Windows Forms specific, however the principle is the same). 可以在MSDN上找到一个允许区分单击和双击的代码示例(特定于Windows窗体,但是原理相同)。 Alternatively, please see this example using DispatcherTimer taken from this previous question
或者,请使用上一个问题中的 DispatcherTimer查看此示例
private static DispatcherTimer clickTimer =
new DispatcherTimer(
TimeSpan.FromMilliseconds(SystemInformation.DoubleClickTime),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
Trace.WriteLine("Double Click");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
Trace.WriteLine("Single Click");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.