add Support for Background Services ...

This commit is contained in:
2026-08-19 12:33:57 +03:30
parent 32dd5dd3c1
commit 4697937709
4 changed files with 359 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace xCommons.Providers
{
/// <summary>
/// Resumable Background Task ...
/// </summary>
public abstract class XResumableBackgroundService : XBackgroundService
{
/// <summary>
/// Check is Paused ...
/// </summary>
protected bool paused = false;
protected XResumableBackgroundService(
IServiceProvider serviceProvider,
ILogger<XResumableBackgroundService> logger
) : base(
logger: logger,
serviceProvider: serviceProvider
)
{ }
protected override async Task ExecuteAsync(
CancellationToken stoppingToken = default
)
{
//
if (paused)
{
return;
}
//
await DoWorkAsync(
payload: null,
cancellationToken: stoppingToken
);
}
/// <summary>
/// do all required works here ...
/// </summary>
/// <param name="payload"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public abstract Task DoWorkAsync(
object payload = null,
CancellationToken cancellationToken = default
);
/// <summary>
/// Pause Consuming ...
/// </summary>
/// <returns></returns>
public bool Pause()
{
//
var result = !paused;
if (!result)
{
return result;
}
//
paused = true;
result = paused;
return result;
}
/// <summary>
/// Resume Consuming ...
/// </summary>
/// <returns></returns>
public bool Resume()
{
//
var result = paused;
if (!result)
{
return result;
}
//
paused = false;
result = !paused;
return result;
}
}
}