Great question... and here's the reason:
"Timing" is a tricky thing when it comes to computers... you can never rely on an "interval" to be perfect. Some computers will 'tick' the timer only every 14 to 15 milliseconds, some more frequently than that, some less frequently.
So:
Thread.Sleep(1);
Could take anywhere from 1 to 30 milliseconds to run.
Instead, if you need a more precise timer - you have to capture the DateTime of when you begin, and then in your timers you would have to check by subtracting DateTime.Now and your original time to see if it is "time to run" your code.
Here's some sample code of what you need to do:
DateTime startDate = DateTime.Now;
Then, start your timer with the interval set to 1 millisecond. Then, in your method:
if (DateTime.Now.Subtract(startDate).TotalSeconds % 3 == 0)
{
// This code will fire every 3 seconds.
}
That code above will fire faithfully every 3 seconds. You can leave it running for 10 years, and it will still fire every 3 seconds.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…