jellyfin/MediaBrowser.Common/ScheduledTasks/IntervalTrigger.cs

79 lines
2.1 KiB
C#
Raw Normal View History

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