Visual Basic StopWatch/Timer Wrong?

jonthedit

Well-Known Member
OP
Member
Joined
May 30, 2011
Messages
1,682
Trophies
0
XP
1,030
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,194
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

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • SylverReZ @ SylverReZ:
    You're welcome.
  • Skelletonike @ Skelletonike:
    I use micro sd 's for bootable windows and even steam os.
    I do have a huge pile of some hentai games from back and mix cd's from back in the day though
  • Skelletonike @ Skelletonike:
    I do need to get a decent, modern tower that supports the bluray/dvd/cd readers

    though
  • Skelletonike @ Skelletonike:
    I hate how barely any cases have

    optical drive bay
  • BigOnYa @ BigOnYa:
    You can buy an external Blu-Ray /DVD/ CD/ SD Card burner/ reader that works with USB. https://www.walmart.com/ip/7-1-Exte...f67f158ddabc1dc363c3721e5a9b772a&gclsrc=3p.ds
  • SylverReZ @ SylverReZ:
    @BigOnYa, External CD/DVD burners are alright, but they're not the best in regards to burning DVDs. You end up with errors, regardless if using cheap or good quality media.
  • SylverReZ @ SylverReZ:
    I typically go with internal SATA drives, preferably a Lite-On or ASUS.
    +1
  • K3Nv3 @ K3Nv3:
    An egg mcmuffin sounds kind of good this morning
  • SylverReZ @ SylverReZ:
    @K3Nv3, I had one yesterday for breakfast, a bacon and egg McMuffin.
  • K3Nv3 @ K3Nv3:
    Haven't ate McDonald's all month
  • BigOnYa @ BigOnYa:
    :O
    Surprised they haven't sent somebody to check on you.
  • K3Nv3 @ K3Nv3:
    Actually haven't really bought anything big this month saved like $600
    +1
  • BigOnYa @ BigOnYa:
    That'd be bout the cost for Switch2 come June, maybe little more if you want a game. Hold on to it.
    +1
  • K3Nv3 @ K3Nv3:
    It'll be gone by next month over a bill I forgot to spend probably
    +1
  • K3Nv3 @ K3Nv3:
    I could easily spend $20 a day on grubhub but eh I can survive off $4 frozen meals
  • K3Nv3 @ K3Nv3:
    You really only need one meal a day and maybe a snack here and there
  • BigOnYa @ BigOnYa:
    Me n wifey used to go out every weekend to dinner, not anymore, shit too expensive. We usually take the grandkids out when we have them, thats pricey enough for us. I like to cook anyways, so make everything at home.
  • K3Nv3 @ K3Nv3:
    cock*
  • BigOnYa @ BigOnYa:
    Hey whatever, if that's what you like, more power to you.
  • K3Nv3 @ K3Nv3:
    It can be your only meal a day just ask
    +1
  • K3Nv3 @ K3Nv3:
    You don't really appreciate left overs tell you get older
    +1
  • BigOnYa @ BigOnYa:
    I make my own pizza from scratch every week..make my own dough, and sauce. Yea ill take leftovers sometimes and put over tater tots, onions, mushroom soup, n cheese and bake, we call it leftover casserole.
  • K3Nv3 @ K3Nv3:
    I use to make hashbrown ground beef and frozen veggies mixed together pretty tasty with ketchup
    +1
  • K3Nv3 @ K3Nv3:
    Kind of like that nix the soup
    +1
    K3Nv3 @ K3Nv3: Kind of like that nix the soup +1