Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Class Form1
    Dim running As Boolean = False
    Dim miliSeconds As Integer = 0, seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        running = True
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        running = False
        Timer1.Stop()
        Timer1.Enabled = False
    End Sub

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

        miliSeconds += 1
        If miliSeconds = 1000 Then
            miliSeconds = 0
            seconds += 1
            If (seconds = 60) Then
                seconds = 0
                minutes += 1
                If (minutes = 60) Then
                    minutes = 0
                    hours += 1
                End If
            End If
        End If
        Label1.Text = hours & ":" & minutes & ":" & seconds & ":" & miliSeconds
    End Sub
End Class



I have used the above code but it's not giving the right milisecond. How to get the right milisecond?
Posted
Comments
ZurdoDev 3-Mar-15 8:22am    
How often is your timer1 set to tick? Also, timers are not guaranteed to run at an accurately specific interval.
dipankarnalui 3-Mar-15 8:30am    
the minimum value in property box of timer is 1. I gave 1 as interval in timer1 property box. how much i have to give timer1.interval ?
ZurdoDev 3-Mar-15 8:35am    
So, why do you say milliseconds are wrong?
dipankarnalui 3-Mar-15 8:40am    
miliseconds are wrong bcoz i'm not getting actual seconds. i'm using Hour:Minute:Second:Milisecond. if miliseconds is correct then why the second is not increasing after 1 second? i'm watching clock. and checking is it showing the second correctly ? no it's not showing. after a long time the second is showing.
ZurdoDev 3-Mar-15 8:48am    
You need to debug it then. You probably need to lock the code. Since you are firing every millisecond and using a global variable you may have timing issues.

Don't do it like that.
Instead, keep a class level DateTime value which you set to DateTime.Now when you start the timer, and generate the elapsed time on the Tick:
VB
Dim diff As TimeSpan = DateTime.Now - startTime
Label1.Text = diff.ToString("hh\:mm\:ss\:fff")

Since Tick events happen at some point after (but not guaranteed to be exactly at) the moment when the timer expires, it's the only way to keep the elapsed time display accurate.

[edit]Code changed to VB[/edit]
 
Share this answer
 
v2
Comments
Maciej Los 3-Mar-15 10:49am    
Question is taged: VB.NET, not C#, Paul ;)
Idea is worth 5!
OriginalGriff 3-Mar-15 10:58am    
So remove the two semicolons! :laugh:
Maciej Los 3-Mar-15 11:03am    
Me? It's your job! :laugh:
Cheers, Maciej
dipankarnalui 3-Mar-15 12:00pm    
thank u all... it's working fine..
OriginalGriff 3-Mar-15 12:04pm    
You're welcome!
VB
Public Class Form1
    Dim running As Boolean = False
    Dim miliSeconds As Integer = 0, seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0
    Dim startTime As DateTime
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        startTime = DateTime.Now
        running = True
        Timer1.Enabled = True
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        running = False
        Timer1.Stop()
        Timer1.Enabled = False
    End Sub

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

        'miliSeconds += 1
        'If miliSeconds = 1000 Then
        '    miliSeconds = 0
        '    seconds += 1
        '    If (seconds = 60) Then
        '        seconds = 0
        '        minutes += 1
        '        If (minutes = 60) Then
        '            minutes = 0
        '            hours += 1
        '        End If
        '    End If
        'End If
        'Label1.Text = hours & ":" & minutes & ":" & seconds & ":" & miliSeconds
        Dim diff As TimeSpan = DateTime.Now - startTime
        Label1.Text = diff.ToString("hh\:mm\:ss\:fff")

    End Sub


End Class



this is working fine. thank u..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900