Jump to content

Timer, not working at right pace?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Skrubber

Skrubber

    Newbie

  • Members
  • Pip
  • 5 posts
Ok, so I experimented a bit with Visual Basic's timer today. I noticed how it didn't seem to be working quite at the right pace. So I went back to making a really simple application.

Here is the code:

Public Class Form1

    Dim Count As Integer = 0

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Count += 1

        TextBox1.Text = Count

    End Sub

End Class

That's all there is to it. I set Timer.Interval to 100 (which means it should go through the code 10 times per second) and Timer.Enabled to True.

Yet, when i start the application, when "Count" reaches 600, my reliable watch shows me that 1:04 seconds have passed.

If I shorten the intervals the timer becomes even less precise.

Is this a known problem? My system clock seems to be working just fine and i have a new Vaio, so i doubt it's the computer's fault.

Please help me! :confused:

Edited by Jaan, 15 November 2009 - 12:17 PM.
Please use code tags when you are posting your codes !


#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Then your computer is too slow for that. Then you have to use dates and Timespans together with the Timer, do you know how to use them or do you want me to show you how they works?

#3
Skrubber

Skrubber

    Newbie

  • Members
  • Pip
  • 5 posts

Vswe said:

Then your computer is too slow for that.
Don't really see how a modern computer wouldn't be able to run the code, I presented, at full speed? Especially when keeping in mind that my computer can run Crysis on low settings at a very decent speed.

But I'll look up timespans, thank you a lot. :)

#4
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
The thing that takes the time is to modify the text of your from, without the change there it would go faster. However the delay is about 6.5 milliseconds/tick so if you run programs that has that delay you won't normally notice that.

#5
Skrubber

Skrubber

    Newbie

  • Members
  • Pip
  • 5 posts
Ok, so tried the same code on another computer. Got just the same problem.

Going back to what you wrote before...

Vswe said:

Then you have to use dates and Timespans together with the Timer, do you know how to use them or do you want me to show you how they works?

Do you mean that, every time the timer runs it should only go through the code if the condition
tid1.time - current time <= x is true?

I think that would work good when actually presenting time on the screen, but since the intervals wouldn't be equally long, would it really work well when making, for example, a game?

#6
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I meant you could do something like this, note that this is only a simple example and if you use it you should use some better names :)

    Dim StartTime As Date
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        StartTime = Now
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim SinceStart As TimeSpan = Now - StartTime
        Label1.Text = SinceStart.Hours & ":" & SinceStart.Minutes & ":" & SinceStart.Seconds
    End Sub


#7
Skrubber

Skrubber

    Newbie

  • Members
  • Pip
  • 5 posts
But let's say I don't want to present time, but rather do something more gameish? I copied a short part from one of my projects as an example:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If Form1.RightArrow = True Then
            move.OneStepRight()
        End If

    End Sub

The timer1.interval is set to 100, meaning it should do this 10 times a second. So is there any way to ensure it actually does this 10 times a second, so it doesn't end up being out of sync like the first example i showed?

#8
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You can tell it to do something ten times a second but you can't really be sure it actually have time to do it. :)

#9
Skrubber

Skrubber

    Newbie

  • Members
  • Pip
  • 5 posts
But, i mean... i can play a game with a very stable 60fps, but i can't increase a single variables worth, by one point, ten times a second? May it be that Visual Studio's not at all i suited for game programming? :(

#10
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Yes, Visual Basic isn't really designed for games.

#11
n00bl3z-c0rp

n00bl3z-c0rp

    Newbie

  • Members
  • Pip
  • 2 posts

Skrubber said:

Ok, so I experimented a bit with Visual Basic's timer today. I noticed how it didn't seem to be working quite at the right pace. So I went back to making a really simple application.

Here is the code:

Public Class Form1


    Dim Count As Integer = 0


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


        Count += 1


        TextBox1.Text = Count


    End Sub


End Class


That's all there is to it. I set Timer.Interval to 100 (which means it should go through the code 10 times per second) and Timer.Enabled to True.

Yet, when i start the application, when "Count" reaches 600, my reliable watch shows me that 1:04 seconds have passed.

If I shorten the intervals the timer becomes even less precise.

Is this a known problem? My system clock seems to be working just fine and i have a new Vaio, so i doubt it's the computer's fault.

Please help me! :confused:
As the first reply said, i put this into VB 08, and 2010 and it worked fine for both.

#12
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Didn't you get a delay of some millisecond for each tick?