diff --git a/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs b/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs index ffb186632c..bb6b613afd 100644 --- a/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs +++ b/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs @@ -884,14 +884,6 @@ namespace Umbraco.Tests.Scheduling } public override bool RunsOnShutdown { get { return false; } } - - protected override void Dispose(bool disposing) - { - Disposed = true; - base.Dispose(disposing); - } - - public bool Disposed { get; private set; } } private class MyTask : BaseTask diff --git a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs index fa6efae5b3..9612ede0b9 100644 --- a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs +++ b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs @@ -90,7 +90,8 @@ namespace Umbraco.Web.Scheduling _options = options; _logPrefix = "[" + name + "] "; - HostingEnvironment.RegisterObject(this); + if (options.Hosted) + HostingEnvironment.RegisterObject(this); if (options.AutoStart) StartUp(); diff --git a/src/Umbraco.Web/Scheduling/BackgroundTaskRunnerOptions.cs b/src/Umbraco.Web/Scheduling/BackgroundTaskRunnerOptions.cs index 4688ff37d6..55df42d3b7 100644 --- a/src/Umbraco.Web/Scheduling/BackgroundTaskRunnerOptions.cs +++ b/src/Umbraco.Web/Scheduling/BackgroundTaskRunnerOptions.cs @@ -15,6 +15,8 @@ namespace Umbraco.Web.Scheduling LongRunning = false; KeepAlive = false; AutoStart = false; + PreserveRunningTask = false; + Hosted = true; } /// @@ -36,9 +38,16 @@ namespace Umbraco.Web.Scheduling public bool AutoStart { get; set; } /// - /// Gets or setes a value indicating whether the running task should be preserved + /// Gets or sets a value indicating whether the running task should be preserved /// once completed, or reset to null. For unit tests. /// public bool PreserveRunningTask { get; set; } + + /// + /// Gets or sets a value indicating whether the runner should register with (and be + /// stopped by) the hosting. Otherwise, something else should take care of stopping + /// the runner. True by default. + /// + public bool Hosted { get; set; } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs b/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs index c024382ee8..1fdb72bbb0 100644 --- a/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs +++ b/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs @@ -63,15 +63,22 @@ namespace Umbraco.Web.Scheduling protected virtual void Dispose(bool disposing) { - // lock on _latch instead of creating a new object as _timer is + if (disposing == false) return; + + // lock on _latch instead of creating a new object as _latch is // private, non-null, readonly - so safe here lock (_latch) { if (_disposed) return; _disposed = true; - - _latch.Dispose(); + _latch.Dispose(); + DisposeResources(); } } + + protected virtual void DisposeResources() + { } + + public bool Disposed { get { return _disposed; } } } } diff --git a/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs b/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs index 3fea70a2b8..567f85f1f5 100644 --- a/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs +++ b/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs @@ -84,19 +84,13 @@ namespace Umbraco.Web.Scheduling /// and returning a value indicating whether to repeat the task. public abstract Task PerformRunAsync(CancellationToken token); - protected override void Dispose(bool disposing) + protected override void DisposeResources() { - // lock on _timer instead of creating a new object as _timer is - // private, non-null, readonly - so safe here - lock (_timer) - { - if (_disposed) return; - _disposed = true; + base.DisposeResources(); - // stop the timer - _timer.Change(Timeout.Infinite, Timeout.Infinite); - _timer.Dispose(); - } + // stop the timer + _timer.Change(Timeout.Infinite, Timeout.Infinite); + _timer.Dispose(); } } } \ No newline at end of file