Files
Umbraco-CMS/src/Umbraco.Web/Scheduling/LatchedBackgroundTaskBase.cs

62 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using System.Threading.Tasks;
2015-07-14 15:55:56 +02:00
using Umbraco.Core;
namespace Umbraco.Web.Scheduling
{
2018-03-27 10:04:07 +02:00
public abstract class LatchedBackgroundTaskBase : DisposableObjectSlim, ILatchedBackgroundTask
{
private TaskCompletionSource<bool> _latch;
protected LatchedBackgroundTaskBase()
{
_latch = new TaskCompletionSource<bool>();
}
/// <summary>
/// Implements IBackgroundTask.Run().
/// </summary>
2017-05-30 10:50:09 +02:00
public virtual void Run()
{
throw new NotSupportedException("This task cannot run synchronously.");
}
/// <summary>
/// Implements IBackgroundTask.RunAsync().
/// </summary>
2017-05-30 10:50:09 +02:00
public virtual Task RunAsync(CancellationToken token)
{
throw new NotSupportedException("This task cannot run asynchronously.");
}
/// <summary>
/// Indicates whether the background task can run asynchronously.
/// </summary>
public abstract bool IsAsync { get; }
2017-05-30 10:50:09 +02:00
public Task Latch => _latch.Task;
2017-05-30 10:50:09 +02:00
public bool IsLatched => _latch.Task.IsCompleted == false;
protected void Release()
{
_latch.SetResult(true);
}
protected void Reset()
{
_latch = new TaskCompletionSource<bool>();
}
2017-05-30 10:50:09 +02:00
public virtual bool RunsOnShutdown => false;
2015-07-14 15:55:56 +02:00
// the task is going to be disposed after execution,
// unless it is latched again, thus indicating it wants to
// remain active
2015-07-14 15:55:56 +02:00
protected override void DisposeResources()
{ }
}
}