JumpyNET Posted August 3, 2006 Posted August 3, 2006 Is it possible to create a deterministic timer? I need to fire an event every 40 milliseconds. It doesn't matter if the event doesn't start at the precise millisecond. But it is necessary that the event is run once and only once during every 40 milliseconds passed. The built in timer doesn't seem to be very accurate. If I use a 40 ms timer it runs only 21 times in a second and not 25 like it should. :confused: Any ideas? Quote
NeuralJack Posted August 3, 2006 Posted August 3, 2006 I'm just guessing but a lot of that could be because 40 ms is an extremely short amount of time. Your computer may not be fast enough to fire that event every 40 ms consistently. You could be right that the internal clock probably doesnt update windows timers as often as we'd like at all times. Is it possible to create a deterministic timer? I need to fire an event every 40 milliseconds. It doesn't matter if the event doesn't start at the precise millisecond. But it is necessary that the event is run once and only once during every 40 milliseconds passed. The built in timer doesn't seem to be very accurate. If I use a 40 ms timer it runs only 21 times in a second and not 25 like it should. :confused: Any ideas? Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Cags Posted August 3, 2006 Posted August 3, 2006 Which Timer class are you using? System.Windows.Forms.Timer is only accurate to about 55ms. http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/ Quote Anybody looking for a graduate programmer (Midlands, England)?
JumpyNET Posted August 4, 2006 Author Posted August 4, 2006 Which Timer class are you using? System.Windows.Forms.Timer is only accurate to about 55ms. http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/ I did not even realize that there are different timers! :eek: Quote
JumpyNET Posted August 4, 2006 Author Posted August 4, 2006 I'm just guessing but a lot of that could be because 40 ms is an extremely short amount of time. Your computer may not be fast enough to fire that event every 40 ms consistently. You could be right that the internal clock probably doesnt update windows timers as often as we'd like at all times. I think my computer is fast enough to run the timer every 40 ms because if I set the timer to run every 1 ms then it runs 65 times in a second which is a bit faster (as if the interval was about 15 ms). Quote
JumpyNET Posted August 4, 2006 Author Posted August 4, 2006 So... So should I use the thread timer? I tried the following but it ended up in an error: Private Sub Button_EnableTimer_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button_EnableTimer.Click Dim t As New System.Threading.Timer(AddressOf TestCallback, Nothing, 0, 40) End Sub Private Sub TestCallback(ByVal state As Object) ListBox1.Items.Add("AA: " & My.Computer.Clock.LocalTime.Second) End Sub Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on. Quote
Administrators PlausiblyDamp Posted August 4, 2006 Administrators Posted August 4, 2006 http://www.grahamwideman.com/gw/tech/dataacq/wintiming.htm http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=85520 http://www.microsoft.com/whdc/system/CEC/mm-timer.mspx Are worth a look as they give some good background information regarding timers and precision plus some suggested alternatives. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.