using System;
using System.Threading;
namespace MediaBrowser.Common.ScheduledTasks
{
///
/// Represents a task trigger that fires everyday
///
public class DailyTrigger : BaseTaskTrigger
{
///
/// Get the time of day to trigger the task to run
///
/// The time of day.
public TimeSpan TimeOfDay { get; set; }
///
/// Gets or sets the timer.
///
/// The timer.
private Timer Timer { get; set; }
///
/// Stars waiting for the trigger action
///
/// if set to true [is application startup].
protected internal override void Start(bool isApplicationStartup)
{
DisposeTimer();
var now = DateTime.Now;
var triggerDate = now.TimeOfDay > TimeOfDay ? now.Date.AddDays(1) : now.Date;
triggerDate = triggerDate.Add(TimeOfDay);
Timer = new Timer(state => OnTriggered(), null, triggerDate - now, TimeSpan.FromMilliseconds(-1));
}
///
/// Stops waiting for the trigger action
///
protected internal override void Stop()
{
DisposeTimer();
}
///
/// Disposes this instance.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool dispose)
{
if (dispose)
{
DisposeTimer();
}
base.Dispose(dispose);
}
///
/// Disposes the timer.
///
private void DisposeTimer()
{
if (Timer != null)
{
Timer.Dispose();
}
}
}
}