From 00de5b1ca7c921bf84b9ba2a51bcf40e4bdafe15 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 29 Jan 2016 14:20:09 -0500 Subject: [PATCH] update use of timers --- MediaBrowser.Dlna/PlayTo/PlayToManager.cs | 24 ++++++------------- .../News/NewsEntryPoint.cs | 6 ++--- .../EntryPoints/KeepServerAwake.cs | 6 ++--- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/MediaBrowser.Dlna/PlayTo/PlayToManager.cs b/MediaBrowser.Dlna/PlayTo/PlayToManager.cs index 6c79007c4e..94f8104be4 100644 --- a/MediaBrowser.Dlna/PlayTo/PlayToManager.cs +++ b/MediaBrowser.Dlna/PlayTo/PlayToManager.cs @@ -36,7 +36,7 @@ namespace MediaBrowser.Dlna.PlayTo private readonly IMediaSourceManager _mediaSourceManager; private readonly List _nonRendererUrls = new List(); - private Timer _clearNonRenderersTimer; + private DateTime _lastRendererClear; public PlayToManager(ILogger logger, ISessionManager sessionManager, ILibraryManager libraryManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost, IImageProcessor imageProcessor, IDeviceDiscovery deviceDiscovery, IHttpClient httpClient, IServerConfigurationManager config, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager) { @@ -57,19 +57,9 @@ namespace MediaBrowser.Dlna.PlayTo public void Start() { - _clearNonRenderersTimer = new Timer(OnClearUrlTimerCallback, null, TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(10)); - _deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered; } - private void OnClearUrlTimerCallback(object state) - { - lock (_nonRendererUrls) - { - _nonRendererUrls.Clear(); - } - } - async void _deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e) { string usn; @@ -99,6 +89,12 @@ namespace MediaBrowser.Dlna.PlayTo lock (_nonRendererUrls) { + if ((DateTime.UtcNow - _lastRendererClear).TotalMinutes >= 10) + { + _nonRendererUrls.Clear(); + _lastRendererClear = DateTime.UtcNow; + } + if (_nonRendererUrls.Contains(location, StringComparer.OrdinalIgnoreCase)) { return; @@ -181,12 +177,6 @@ namespace MediaBrowser.Dlna.PlayTo public void Dispose() { _deviceDiscovery.DeviceDiscovered -= _deviceDiscovery_DeviceDiscovered; - - if (_clearNonRenderersTimer != null) - { - _clearNonRenderersTimer.Dispose(); - _clearNonRenderersTimer = null; - } } } } diff --git a/MediaBrowser.Server.Implementations/News/NewsEntryPoint.cs b/MediaBrowser.Server.Implementations/News/NewsEntryPoint.cs index e8f910f81b..969541fbc8 100644 --- a/MediaBrowser.Server.Implementations/News/NewsEntryPoint.cs +++ b/MediaBrowser.Server.Implementations/News/NewsEntryPoint.cs @@ -1,6 +1,5 @@ using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Notifications; @@ -17,12 +16,13 @@ using System.Threading; using System.Threading.Tasks; using System.Xml; using CommonIO; +using MediaBrowser.Common.Threading; namespace MediaBrowser.Server.Implementations.News { public class NewsEntryPoint : IServerEntryPoint { - private Timer _timer; + private PeriodicTimer _timer; private readonly IHttpClient _httpClient; private readonly IApplicationPaths _appPaths; private readonly IFileSystem _fileSystem; @@ -47,7 +47,7 @@ namespace MediaBrowser.Server.Implementations.News public void Run() { - _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(500), _frequency); + _timer = new PeriodicTimer(OnTimerFired, null, TimeSpan.FromMilliseconds(500), _frequency); } /// diff --git a/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs b/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs index ba335868d3..20d4c6b2a4 100644 --- a/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs +++ b/MediaBrowser.Server.Startup.Common/EntryPoints/KeepServerAwake.cs @@ -4,7 +4,7 @@ using MediaBrowser.Controller.Session; using MediaBrowser.Model.Logging; using System; using System.Linq; -using System.Threading; +using MediaBrowser.Common.Threading; namespace MediaBrowser.Server.Startup.Common.EntryPoints { @@ -12,7 +12,7 @@ namespace MediaBrowser.Server.Startup.Common.EntryPoints { private readonly ISessionManager _sessionManager; private readonly ILogger _logger; - private Timer _timer; + private PeriodicTimer _timer; private readonly IServerApplicationHost _appHost; public KeepServerAwake(ISessionManager sessionManager, ILogger logger, IServerApplicationHost appHost) @@ -24,7 +24,7 @@ namespace MediaBrowser.Server.Startup.Common.EntryPoints public void Run() { - _timer = new Timer(obj => + _timer = new PeriodicTimer(obj => { var now = DateTime.UtcNow; if (_sessionManager.Sessions.Any(i => (now - i.LastActivityDate).TotalMinutes < 15))