IntervalTrigger.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Threading;
  3. namespace MediaBrowser.Common.ScheduledTasks
  4. {
  5. /// <summary>
  6. /// Represents a task trigger that runs repeatedly on an interval
  7. /// </summary>
  8. public class IntervalTrigger : BaseTaskTrigger
  9. {
  10. /// <summary>
  11. /// Gets or sets the interval.
  12. /// </summary>
  13. /// <value>The interval.</value>
  14. public TimeSpan Interval { get; set; }
  15. /// <summary>
  16. /// Gets or sets the timer.
  17. /// </summary>
  18. /// <value>The timer.</value>
  19. private Timer Timer { get; set; }
  20. /// <summary>
  21. /// Stars waiting for the trigger action
  22. /// </summary>
  23. /// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
  24. protected internal override void Start(bool isApplicationStartup)
  25. {
  26. DisposeTimer();
  27. Timer = new Timer(state => OnTriggered(), null, Interval, TimeSpan.FromMilliseconds(-1));
  28. }
  29. /// <summary>
  30. /// Stops waiting for the trigger action
  31. /// </summary>
  32. protected internal override void Stop()
  33. {
  34. DisposeTimer();
  35. }
  36. /// <summary>
  37. /// Disposes this instance.
  38. /// </summary>
  39. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  40. protected override void Dispose(bool dispose)
  41. {
  42. if (dispose)
  43. {
  44. DisposeTimer();
  45. }
  46. base.Dispose(dispose);
  47. }
  48. /// <summary>
  49. /// Disposes the timer.
  50. /// </summary>
  51. private void DisposeTimer()
  52. {
  53. if (Timer != null)
  54. {
  55. Timer.Dispose();
  56. }
  57. }
  58. }
  59. }