WatchDog.cs 939 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace Client.SSE
  8. {
  9. public class Watchdog
  10. {
  11. private long _timeout;
  12. private Timer _timer;
  13. public event EventHandler TimerExpired;
  14. public void Start()
  15. {
  16. _timer = new Timer(new TimerCallback(OnTimerExpired), null, 0, _timeout);
  17. }
  18. public void Reset()
  19. {
  20. _timer.Change(0, _timeout);
  21. }
  22. private void OnTimerExpired(object State)
  23. {
  24. _timer.Change(Timeout.Infinite, Timeout.Infinite);
  25. if (TimerExpired != null)
  26. TimerExpired(this, new EventArgs());
  27. }
  28. public Watchdog(long timeout)
  29. {
  30. if (timeout < 1) throw new ArgumentOutOfRangeException("timeout", "timeout muste be greater than zero.");
  31. }
  32. }
  33. }