Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 1.25 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I use threading to 'tick' a timer to be accessed by other threads?
  2. private DateTime _lastTap;
  3. public void TapHandler()
  4. {
  5.   DateTime now = DateTime.UtcNow;
  6.   TimeSpan span = now - lastTap;
  7.   _lastTap = now;
  8.   if (span < TimeSpan.FromSeconds(1)) {...}
  9. }
  10.        
  11. private Stopwatch _stopwatch = new Stopwatch();
  12. public void TapHandler()
  13. {
  14.   TimeSpan elapsed = _stopwatch.Elapsed;
  15.   _stopwatch.Restart();
  16.   if (elapsed < TimeSpan.FromSeconds(1)) {...}
  17. }
  18.        
  19. private static DispatcherTimer clickTimer =
  20.         new DispatcherTimer(
  21.             TimeSpan.FromMilliseconds(SystemInformation.DoubleClickTime),
  22.             DispatcherPriority.Background,
  23.             mouseWaitTimer_Tick,
  24.             Dispatcher.CurrentDispatcher);
  25.  
  26.     private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  27.     {
  28.         // Stop the timer from ticking.
  29.         myClickWaitTimer.Stop();
  30.  
  31.         Trace.WriteLine("Double Click");
  32.         e.Handled = true;
  33.     }
  34.  
  35.     private void Button_Click(object sender, RoutedEventArgs e)
  36.     {
  37.         myClickWaitTimer.Start();
  38.     }
  39.  
  40.     private static void mouseWaitTimer_Tick(object sender, EventArgs e)
  41.     {
  42.         myClickWaitTimer.Stop();
  43.  
  44.         // Handle Single Click Actions
  45.         Trace.WriteLine("Single Click");
  46.     }