using System; using System.Threading; using System.Threading.Tasks; using Umbraco.Core; namespace Umbraco.Web.Scheduling { internal abstract class LatchedBackgroundTaskBase : DisposableObject, ILatchedBackgroundTask { private TaskCompletionSource _latch; protected LatchedBackgroundTaskBase() { _latch = new TaskCompletionSource(); } /// /// Implements IBackgroundTask.Run(). /// public abstract void Run(); /// /// Implements IBackgroundTask.RunAsync(). /// public abstract Task RunAsync(CancellationToken token); /// /// Indicates whether the background task can run asynchronously. /// public abstract bool IsAsync { get; } public Task Latch { get { return _latch.Task; } } public bool IsLatched { get { return _latch.Task.IsCompleted == false; } } protected void Release() { _latch.SetResult(true); } protected void Reset() { _latch = new TaskCompletionSource(); } public abstract bool RunsOnShutdown { get; } // the task is going to be disposed after execution, // unless it is latched again, thus indicating it wants to // remain active protected override void DisposeResources() { } } }