Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Posted

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?

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

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).

Posted

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.

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...