You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Common/ScheduledTasks/SystemEventTrigger.cs

74 lines
2.3 KiB

using MediaBrowser.Model.Tasks;
using Microsoft.Win32;
12 years ago
using System;
using System.Threading.Tasks;
namespace MediaBrowser.Common.ScheduledTasks
{
/// <summary>
/// Class SystemEventTrigger
/// </summary>
12 years ago
public class SystemEventTrigger : ITaskTrigger
{
/// <summary>
/// Gets or sets the system event.
/// </summary>
/// <value>The system event.</value>
public SystemEvent SystemEvent { get; set; }
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
12 years ago
public void Start(bool isApplicationStartup)
{
switch (SystemEvent)
{
case SystemEvent.WakeFromSleep:
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
break;
}
}
/// <summary>
/// Stops waiting for the trigger action
/// </summary>
12 years ago
public void Stop()
{
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
}
/// <summary>
/// Handles the PowerModeChanged event of the SystemEvents control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PowerModeChangedEventArgs" /> instance containing the event data.</param>
async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Resume && SystemEvent == SystemEvent.WakeFromSleep)
{
// This value is a bit arbitrary, but add a delay to help ensure network connections have been restored before running the task
await Task.Delay(5000).ConfigureAwait(false);
OnTriggered();
}
}
12 years ago
/// <summary>
/// Occurs when [triggered].
/// </summary>
public event EventHandler<EventArgs> Triggered;
/// <summary>
/// Called when [triggered].
/// </summary>
private void OnTriggered()
{
if (Triggered != null)
{
Triggered(this, EventArgs.Empty);
}
}
}
}