Deterministic timer?

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
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?
 
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.



JumpyNET said:
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?
 
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).
 
So...

So should I use the thread timer?

I tried the following but it ended up in an error:

Visual Basic:
    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.
 
Back
Top