I'm using
using PeriodicTimer timer = new(_period);
while (!stoppingToken.IsCancellationRequested
&& await timer.WaitForNextTickAsync(stoppingToken))
{
await DoWork();
}
Is there a way to set _period dynamically? that is, take that period of a database for example.
A new property
PeriodicTimer.Periodhas been introduced on .NET 8:Originally this answer contained a custom
UpdateablePeriodicTimerimplementation, featuring the (then lacking)Periodproperty. In case you are targeting .NET 6 or 7, you can find it in the 1st revision of this answer. I am no longer satisfied with that implementation though, because when I wrote it I was not aware of a by design behavior of thePeriodicTimerclass: