using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace xCommons.Providers
{
///
/// Resumable Background Task ...
///
public abstract class XResumableBackgroundService : XBackgroundService
{
///
/// Check is Paused ...
///
protected bool paused = false;
protected XResumableBackgroundService(
IServiceProvider serviceProvider,
ILogger logger
) : base(
logger: logger,
serviceProvider: serviceProvider
)
{ }
protected override async Task ExecuteAsync(
CancellationToken stoppingToken = default
)
{
//
if (paused)
{
return;
}
//
await DoWorkAsync(
payload: null,
cancellationToken: stoppingToken
);
}
///
/// do all required works here ...
///
///
///
///
public abstract Task DoWorkAsync(
object payload = null,
CancellationToken cancellationToken = default
);
///
/// Pause Consuming ...
///
///
public bool Pause()
{
//
var result = !paused;
if (!result)
{
return result;
}
//
paused = true;
result = paused;
return result;
}
///
/// Resume Consuming ...
///
///
public bool Resume()
{
//
var result = paused;
if (!result)
{
return result;
}
//
paused = false;
result = !paused;
return result;
}
}
}