using System.Threading.Tasks;
private static readonly TimeSpan _period = TimeSpan.FromMilliseconds(500);
private static readonly CancellationTokenSource _stopper = new CancellationTokenSource();
private static int _x = 1;
public static async Task Main()
var stoppingToken = _stopper.Token;
using PeriodicTimer timer = new(_period);
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
private static ValueTask DoWork()
Console.WriteLine($"{_x}:{DateTimeOffset.Now:mm:ss.ffff}");
return ValueTask.CompletedTask;