What I am trying to do is set a timer going which is set off by an output from an external signal from a machine tool.
At present the code is activated through a button on my form.
This is the code I am using
Public Class Form1
Private CompTime As System.Int32
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CompTime = Environment.TickCount
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label2.Text = TimeOfDay
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Dim CurrentTickValue As System.Int32 = Environment.TickCount
Dim Difference As System.Int32 = CurrentTickValue - CompTime
Dim Hours As System.Int32
Dim Minutes As System.Int32
Dim Seconds As System.Int32
Hours = (Difference / (3600 * 1000)) Mod 24
Minutes = (Difference / (60 * 1000)) Mod 60
Seconds = (Difference / 1000) Mod 60
Label1.Text = String.Format("This application has been running for " _
& Hours & " Hours " & Minutes & " Minutes" & Seconds & " Seconds")
End Sub
Private Sub TimerStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerStart.Click
Timer2.Enabled = True
End Sub
Private Sub Timerstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timerstop.Click
Timer2.Enabled = False
End Sub
End Class
The problem I am having is when the timer reaches 30 seconds a minute is added. Then after a further 60 seconds another minute is added. At first I thought the minutes were being rounded up but if I change the minutes calulation
from this
Minutes = (Difference / (60 * 1000)) Mod 60
to this
Minutes = (Difference / (120 * 1000)) Mod 60
Then it will add a minute after 60 seconds have gone by which is correct.
I'm sure this is something simple but I can not see what the problem is. Logic says the calculation is correct but obviously not. I have not tried the hours yet will cross that bridge when I get to it.
Hope you can help
regards
Sharpy

New Topic/Question
Reply



MultiQuote




|