ResettableTimer.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Timers;
  7. namespace LogoForceTestApp.Modules.MainModule.Models
  8. {
  9. public class ResettableTimer
  10. {
  11. private readonly Timer _timer;
  12. private ElapsedEventHandler _onIntervalElapsed;
  13. public event ElapsedEventHandler Elapsed
  14. {
  15. add
  16. {
  17. _onIntervalElapsed = (ElapsedEventHandler)Delegate.Combine(_onIntervalElapsed, value);
  18. }
  19. remove
  20. {
  21. _onIntervalElapsed = (ElapsedEventHandler)Delegate.Remove(_onIntervalElapsed, value);
  22. }
  23. }
  24. public ResettableTimer(double interval)
  25. {
  26. _timer = new Timer(interval);
  27. _timer.Elapsed += OnTimedEvent;
  28. }
  29. private void OnTimedEvent(object source, ElapsedEventArgs e)
  30. {
  31. //Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}", e.SignalTime);
  32. _onIntervalElapsed.Invoke(this, e);
  33. }
  34. public void Start()
  35. {
  36. _timer.Start();
  37. }
  38. public void Stop()
  39. {
  40. _timer.Stop();
  41. }
  42. public void Reset()
  43. {
  44. Stop();
  45. Start();
  46. }
  47. }
  48. }