简体   繁体   English

如何使用线程来“勾号”其他线程可以访问的计时器?

[英]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.

相关问题 如何使用带有计时器刻度的BackgroundWorker? - How can i use a BackgroundWorker with a timer tick? 我可以从一个线程使用System.Threading.Timer启动多少个线程(计时器)? - How many threads (timers) can I launch using System.Threading.Timer from one thread? C#如何获取计时器以正确打勾? - C# How can I get timer to tick properly? 我可以停止System.Threading.Timer - Can I stop a System.Threading.Timer 我在计时器滴答事件中使用位图,但一切进展都很缓慢,我该如何解决? - I'm Using Bitmap inside a Timer tick event and everything goes very slow how can i solve it? C#-在计时器刻度上执行多个线程 - c# - Execute multiple threads on timer tick 为什么我不能在WPF中通过System.Threading.Timer调用使用CapturePhotoToStreamAsync? - Why can't I use CapturePhotoToStreamAsync from a System.Threading.Timer call back in WPF? 如何在计时器滴答事件中使网站页面/ URL地址每X秒自动刷新一次? - How can i make a website page/url address to be refresh automatic every X seconds in a timer tick event? 如何根据计时器的每个刻度更改变量(dy)的值? - how can I change the value of a variable(dy) based on each tick of the timer? 我需要在计时器滴答内的按钮单击对象上使用if语句? - I need to use a if statement on a button click object within a timer tick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM