using MediaBrowser.Model.Tasks;
using Microsoft.Win32;
using System.Threading.Tasks;
namespace MediaBrowser.Common.ScheduledTasks
{
///
/// Class SystemEventTrigger
///
public class SystemEventTrigger : BaseTaskTrigger
{
///
/// Gets or sets the system event.
///
/// The system event.
public SystemEvent SystemEvent { get; set; }
///
/// Stars waiting for the trigger action
///
protected internal override void Start()
{
switch (SystemEvent)
{
case SystemEvent.WakeFromSleep:
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
break;
}
}
///
/// Stops waiting for the trigger action
///
protected internal override void Stop()
{
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
}
///
/// Handles the PowerModeChanged event of the SystemEvents control.
///
/// The source of the event.
/// The instance containing the event data.
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();
}
}
}
}