From 028bd5251c10b0094d291101de2eab33a2c4fa2f Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 21 May 2013 22:39:46 -0700 Subject: [PATCH] added cancellation token to Scheduler. --- NzbDrone.Core/Jobs/Scheduler.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/NzbDrone.Core/Jobs/Scheduler.cs b/NzbDrone.Core/Jobs/Scheduler.cs index 9a90a1edf..77a9e6df0 100644 --- a/NzbDrone.Core/Jobs/Scheduler.cs +++ b/NzbDrone.Core/Jobs/Scheduler.cs @@ -1,9 +1,11 @@ using System; -using System.Timers; +using System.Threading; +using System.Threading.Tasks; using NLog; using NzbDrone.Common.Composition; using NzbDrone.Common.Messaging; using NzbDrone.Core.Lifecycle; +using Timer = System.Timers.Timer; namespace NzbDrone.Core.Jobs { @@ -16,6 +18,7 @@ namespace NzbDrone.Core.Jobs private readonly IMessageAggregator _messageAggregator; private readonly Logger _logger; private static readonly Timer Timer = new Timer(); + private static CancellationTokenSource _cancellationTokenSource; public Scheduler(ITaskManager taskManager, IMessageAggregator messageAggregator, Logger logger) { @@ -26,8 +29,9 @@ namespace NzbDrone.Core.Jobs public void Handle(ApplicationStartedEvent message) { + _cancellationTokenSource = new CancellationTokenSource(); Timer.Interval = 1000 * 30; - Timer.Elapsed += (o, args) => ExecuteCommands(); + Timer.Elapsed += (o, args) => Task.Factory.StartNew(ExecuteCommands, _cancellationTokenSource.Token); Timer.Start(); } @@ -43,6 +47,8 @@ namespace NzbDrone.Core.Jobs foreach (var task in tasks) { + _cancellationTokenSource.Token.ThrowIfCancellationRequested(); + try { _messageAggregator.PublishCommand(task.TypeName); @@ -55,12 +61,16 @@ namespace NzbDrone.Core.Jobs } finally { - Timer.Enabled = true; + if (!_cancellationTokenSource.IsCancellationRequested) + { + Timer.Enabled = true; + } } } public void Handle(ApplicationShutdownRequested message) { + _cancellationTokenSource.Cancel(true); Timer.Stop(); } }