first commit -push

This commit is contained in:
dungtt
2025-10-15 15:15:53 +07:00
parent 674ae395be
commit a9577c5756
885 changed files with 74595 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
using System.Diagnostics;
namespace RobotNet.ScriptManager.Services;
public abstract class LoopService(int interval) : IHostedService
{
private readonly TimeSpan Interval = TimeSpan.FromMilliseconds(interval);
private readonly CancellationTokenSource cancellationTokenSource = new();
private readonly Stopwatch stopwatch = new();
private Timer? timer;
public async Task StartAsync(CancellationToken cancellationToken)
{
await BeforExecuteAsync(cancellationToken);
stopwatch.Start();
timer = new Timer(Callback, cancellationTokenSource, 0, Timeout.Infinite);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
cancellationTokenSource.Cancel();
timer?.Dispose();
await AfterExecuteAsync(cancellationToken);
}
protected virtual Task BeforExecuteAsync(CancellationToken cancellationToken) => Task.CompletedTask;
protected abstract void Execute(CancellationToken stoppingToken);
protected virtual Task AfterExecuteAsync(CancellationToken cancellationToken) => Task.CompletedTask;
private void Callback(object? state)
{
if (state is not CancellationTokenSource cts || cts.IsCancellationRequested) return;
Execute(cts.Token);
if (!cts.IsCancellationRequested)
{
long nextTrigger = Interval.Ticks - (stopwatch.ElapsedTicks % Interval.Ticks);
if (nextTrigger <= 0) nextTrigger = Interval.Ticks;
int nextIntervalMs = (int)(nextTrigger / TimeSpan.TicksPerMillisecond);
timer?.Change(nextIntervalMs, Timeout.Infinite);
}
}
}