aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-02-01 14:24:15 -0500
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-02-01 14:24:15 -0500
commit37352785acef4db99301c1e88624cf48133ec979 (patch)
tree15638d697cc56a0a774bf261de9814c9034f8e42 /MediaBrowser.Common
parentf14e9b8d3af98f6ffbe243b105f96f46e8cf3b06 (diff)
parentf5ebeddbf5104092ce584486689af6640125054f (diff)
Merge branch 'beta'
Diffstat (limited to 'MediaBrowser.Common')
-rw-r--r--MediaBrowser.Common/MediaBrowser.Common.csproj1
-rw-r--r--MediaBrowser.Common/Threading/PeriodicTimer.cs72
2 files changed, 73 insertions, 0 deletions
diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj
index ccdb319fee..17f211d848 100644
--- a/MediaBrowser.Common/MediaBrowser.Common.csproj
+++ b/MediaBrowser.Common/MediaBrowser.Common.csproj
@@ -90,6 +90,7 @@
<Compile Include="Security\IRequiresRegistration.cs" />
<Compile Include="Security\ISecurityManager.cs" />
<Compile Include="Security\PaymentRequiredException.cs" />
+ <Compile Include="Threading\PeriodicTimer.cs" />
<Compile Include="Updates\IInstallationManager.cs" />
<Compile Include="Updates\InstallationEventArgs.cs" />
<Compile Include="Updates\InstallationFailedEventArgs.cs" />
diff --git a/MediaBrowser.Common/Threading/PeriodicTimer.cs b/MediaBrowser.Common/Threading/PeriodicTimer.cs
new file mode 100644
index 0000000000..75ccada4ef
--- /dev/null
+++ b/MediaBrowser.Common/Threading/PeriodicTimer.cs
@@ -0,0 +1,72 @@
+using System;
+using System.Threading;
+using Microsoft.Win32;
+
+namespace MediaBrowser.Common.Threading
+{
+ public class PeriodicTimer : IDisposable
+ {
+ public Action<object> Callback { get; set; }
+ private Timer _timer;
+ private readonly object _state;
+ private readonly object _timerLock = new object();
+ private readonly TimeSpan _period;
+
+ public PeriodicTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)
+ {
+ if (callback == null)
+ {
+ throw new ArgumentNullException("callback");
+ }
+
+ Callback = callback;
+ _period = period;
+ _state = state;
+
+ StartTimer(dueTime);
+ }
+
+ void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
+ {
+ if (e.Mode == PowerModes.Resume)
+ {
+ DisposeTimer();
+ StartTimer(Timeout.InfiniteTimeSpan);
+ }
+ }
+
+ private void TimerCallback(object state)
+ {
+ Callback(state);
+ }
+
+ private void StartTimer(TimeSpan dueTime)
+ {
+ lock (_timerLock)
+ {
+ _timer = new Timer(TimerCallback, _state, dueTime, _period);
+
+ SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
+ }
+ }
+
+ private void DisposeTimer()
+ {
+ SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
+
+ lock (_timerLock)
+ {
+ if (_timer != null)
+ {
+ _timer.Dispose();
+ _timer = null;
+ }
+ }
+ }
+
+ public void Dispose()
+ {
+ DisposeTimer();
+ }
+ }
+}