I have a timer that sends data to a text file every 5 minutes of the hour. The data, however, gets sent 1 second later every time. For example, it will send at 10:05:36 one time, then the next time it will send at 10:05:37. Is there a way I can fix it so it sends at the same increment?
Sleep specifies a minimum amount of time to sleep for. It only checks that at least that much time has passed before resuming. It is perfectly alright for it to wait longer than you specify. Since you are not using a real-time OS, there is no way to guarantee an exact amount of time.
DateTime last = DateTime.Now;
for (; ; )
{
while (DateTime.Now - last < TimeSpan.FromMinutes(5))
{
Thread.Sleep(100); //sleep a bit to not saturate the CPU
}
last = DateTime.Now;
SendMyData(last.ToString());
}
Bookmarks