Skip to content

Instantly share code, notes, and snippets.

@GrillPhil
Last active May 17, 2023 09:50
Show Gist options
  • Save GrillPhil/196f0aba7255b988a9f8ad9ff66e71fe to your computer and use it in GitHub Desktop.
Save GrillPhil/196f0aba7255b988a9f8ad9ff66e71fe to your computer and use it in GitHub Desktop.
private int _executionCount = 0;
public bool IsEnabled { get; set; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using PeriodicTimer timer = new PeriodicTimer(_period);
while (
!stoppingToken.IsCancellationRequested &&
await timer.WaitForNextTickAsync(stoppingToken))
{
try
{
if (IsEnabled)
{
_executionCount++;
_logger.LogInformation(
$"Executed PeriodicHostedService - Count: {_executionCount}");
}
else
{
_logger.LogInformation(
"Skipped PeriodicHostedService");
}
}
catch (Exception ex)
{
_logger.LogInformation(
$"Failed to execute PeriodicHostedService with exception message {ex.Message}. Good luck next round!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment