ResetableTimer.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace Taobao.Top.Link
  6. {
  7. /// <summary>easy timer impl
  8. /// </summary>
  9. public class ResetableTimer
  10. {
  11. //min=50ms by .net impl
  12. private Timer _timer;
  13. private int _periodMillisecond;
  14. public event EventHandler Elapsed;
  15. public ResetableTimer(int periodMillisecond)
  16. {
  17. this._periodMillisecond = periodMillisecond;
  18. this._timer = new Timer(o =>
  19. {
  20. if (Elapsed != null)
  21. {
  22. this.Elapsed(null, null);
  23. }
  24. }, null
  25. , this._periodMillisecond
  26. , this._periodMillisecond);
  27. }
  28. /// <summary>cancel timer
  29. /// </summary>
  30. public void Cancel()
  31. {
  32. if (this._timer == null)
  33. return;
  34. this._timer.Dispose();
  35. this._timer = null;
  36. }
  37. /// <summary>delay in period
  38. /// </summary>
  39. public void Delay()
  40. {
  41. this._timer.Change(this._periodMillisecond, this._periodMillisecond);
  42. }
  43. }
  44. }