using System;
using System.Threading;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks
{
///
/// Represents a task trigger that runs repeatedly on an interval
///
public class IntervalTrigger : ITaskTrigger
{
///
/// Gets or sets the interval.
///
/// The interval.
public TimeSpan Interval { get; set; }
///
/// Gets or sets the timer.
///
/// The timer.
private Timer Timer { get; set; }
///
/// Gets the execution properties of this task.
///
///
/// The execution properties of this task.
///
public TaskExecutionOptions TaskOptions { get; set; }
///
/// Stars waiting for the trigger action
///
/// if set to true [is application startup].
public void Start(bool isApplicationStartup)
{
DisposeTimer();
Timer = new Timer(state => OnTriggered(), null, Interval, TimeSpan.FromMilliseconds(-1));
}
///
/// Stops waiting for the trigger action
///
public void Stop()
{
DisposeTimer();
}
///
/// Disposes the timer.
///
private void DisposeTimer()
{
if (Timer != null)
{
Timer.Dispose();
}
}
///
/// Occurs when [triggered].
///
public event EventHandler> Triggered;
///
/// Called when [triggered].
///
private void OnTriggered()
{
if (Triggered != null)
{
Triggered(this, new GenericEventArgs(TaskOptions));
}
}
}
}