C
C#13mo ago
Pandetthe

❔ Task scheduling

Hi,I have a question about the example of code in .net docs for asp.net core 6.0. Is this code good about maintaining high accuracy per minute for a long time?
namespace TimedBackgroundTasks;

public class TimedHostedService : BackgroundService
{
private readonly ILogger<TimedHostedService> _logger;
private int _executionCount;

public TimedHostedService(ILogger<TimedHostedService> logger)
{
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service running.");

// When the timer should have no due-time, then do the work once now.
DoWork();

using PeriodicTimer timer = new(TimeSpan.FromSeconds(1));

try
{
while (await timer.WaitForNextTickAsync(stoppingToken))
{
DoWork();
}
}
catch (OperationCanceledException)
{
_logger.LogInformation("Timed Hosted Service is stopping.");
}
}

// Could also be a async method, that can be awaited in ExecuteAsync above
private void DoWork()
{
int count = Interlocked.Increment(ref _executionCount);

_logger.LogInformation("Timed Hosted Service is working. Count: {Count}", count);
}
}
namespace TimedBackgroundTasks;

public class TimedHostedService : BackgroundService
{
private readonly ILogger<TimedHostedService> _logger;
private int _executionCount;

public TimedHostedService(ILogger<TimedHostedService> logger)
{
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service running.");

// When the timer should have no due-time, then do the work once now.
DoWork();

using PeriodicTimer timer = new(TimeSpan.FromSeconds(1));

try
{
while (await timer.WaitForNextTickAsync(stoppingToken))
{
DoWork();
}
}
catch (OperationCanceledException)
{
_logger.LogInformation("Timed Hosted Service is stopping.");
}
}

// Could also be a async method, that can be awaited in ExecuteAsync above
private void DoWork()
{
int count = Interlocked.Increment(ref _executionCount);

_logger.LogInformation("Timed Hosted Service is working. Count: {Count}", count);
}
}
6 Replies
Pobiega
Pobiega13mo ago
Looks like you're trying to make a scheduler. I can recommend Coravel if you want a lightweight in-process scheduler, and Quartz if you want something more robust (persistance, multiple instances etc)
Pandetthe
Pandetthe13mo ago
I was considering choosing quartz, but I think Coravel will be better for my project Thanks But I am still curious about my previous question, how accurate is this code?
Pobiega
Pobiega13mo ago
¯\_(ツ)_/¯
Pandetthe
Pandetthe13mo ago
I check coravel code and it's using similar code but instead of periodic timer, they used timer
Pobiega
Pobiega13mo ago
coravel was written long before PeriodicTimer so thats probably why
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts