You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Hosting/Workers/ScheduledWorker.cs

34 lines
617 B

2 years ago
namespace Connected.Hosting.Workers
{
public abstract class ScheduledWorker : Worker
{
protected virtual TimeSpan Timer { get; set; } = TimeSpan.FromMinutes(1);
protected long Count { get; private set; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
do
{
try
{
await OnInvoke(stoppingToken);
}
catch (Exception ex)
{
await OnError(ex);
}
finally
{
Count++;
}
if (Timer == TimeSpan.Zero)
break;
await Task.Delay(Timer, stoppingToken);
}
while (!stoppingToken.IsCancellationRequested);
}
}
}