I am storing OHLC data real-time in a CSV file for which I am using a System.Timers.Timer to trigger every 60 seconds. Timer is something like this:
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 60000;
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += TimeElapsed;
timer.Start();
private void TimeElapsed(object sender, System.Timers.Timer.ElapsedEventArgs e){
// Do some Processing here
}
I have been using a Serilog logger to log the output each time this event is triggered and notices one thing that the timer is adding a bit delay (a few milliseconds after every call) and then since the code is running for a long time now like close to 2 days that milliseconds accumulate to make few seconds now. First Entry was at 2023-09-20 21:17:00.363 and now since it is being called every minute, now the call every minute is happening at 2023-09-22 13:46:27.504. Notice an addition of 27 seconds over time. I don't want that. So is there a way to map timer with your windows clock? Or can you suggest a better approach of doing that? Do note that I want it to store in the csv file @HH:MM:00 time only.