Visual Basic StopWatch/Timer Wrong?

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
989
Country
Bangladesh
Hi, not sure how this possible but a stopwatch set to 1000
and a timer set to go up 1 second are actually not perfectly in sync!
I know VB is horrible, but for this project it will work fine.
Here is the code and the .exe
Code:
Public Class Form1
    Public hit2 As Boolean
    Dim fg As Integer
    Dim songtime As Double
    Dim sw As New Stopwatch
    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'If Not e.Control Then Exit Sub
        Select Case e.KeyCode
            Case Keys.D : HitRight()
            Case Keys.A : HitLeft()
        End Select
    End Sub
    Public Sub HitRight()
        LabelLeft.Text = sw.ElapsedMilliseconds
    End Sub
    Public Sub HitLeft()
        'checknoteintegrity()
        LabelLeft.Text = Format(songtime, "0.00")
    End Sub
    Private Sub Timer2_Tick_1(sender As Object, e As EventArgs) Handles Timer2.Tick
        fg = fg + 1
        LabelRight.Text = fg.ToString
        LabelLeft.Text = sw.ElapsedMilliseconds
    End Sub
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer2.Start()
        sw.Start()
        Threading.Thread.Sleep(1000)
    End Sub
End Class
Ignore some of the bits about key presses, not needed for now.

download:
https://mega.co.nz/#!RYB1zYbL!tNFtg9kWojt1ASKzKYjRj8uDPKe3JnlL1T6VQpvXA3Q

alt download
http://s000.tinyupload.com/index.php?file_id=95210156442717472759
 

daxtsu

Well-Known Member
Member
Joined
Jun 9, 2007
Messages
5,627
Trophies
2
XP
5,145
Country
Antarctica
There are several problems here:

1. That's not the entire source code, so it won't compile as it is (where are your using (import in vb.net) directives?).
2. Timers (both System.Windows.Forms.Timer and the Stopwatch class) don't need a Sleep function to be called for them, they run when the OS schedules them to, and using Sleep is generally considered bad practice as far as I know.
3. System.Windows.Forms.Timer uses a very inacccurate timer (it can be out of touch with reality by up to +/- 100milliseconds if I recall, so it's not very accurate), while Stopwatch uses the high precision timer available to Windows, so it's not really a surprise if they aren't in perfect sync.
 
  • Like
Reactions: jonthedit
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys