My C# application has to execute a task every few seconds. It is very important that the execution happens at exactly this interval; give or take a few milliseconds.
I tried using a Timer but the time gradually shifts after a few minutes. The code used by me is as follows:
System.Timers.Timer timerObj = new System.Timers.Timer(10 * 1000);
timerObj.Elapsed += timerObj_Elapsed;
timerObj.AutoReset = true;
timerObj.Start();
static void timerObj_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime currentTime = DateTime.Now;
Console.WriteLine(currentTime.ToString("HH:mm:ss.fff"));
}
Is there a better way to do this kind of activity?