Last active
May 17, 2023 09:50
-
-
Save GrillPhil/196f0aba7255b988a9f8ad9ff66e71fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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