I’m using a Threading.DispatcherTimer and every tick of the timer runs a subroutine.
Is it possible that if the subroutine takes longer then 1s for the processor to finish that it will mess up the time of the timer that is counting in seconds?
Try
timercount = Nothing
timercount = New DispatcherTimer()
timercount.Interval = TimeSpan.FromSeconds(1)
timercount.Start()
AddHandler timercount.Tick, AddressOf TickMe 'Every Second the 'TickMe' Method runs
Catch ex As Exception
' MessageBox.Show(ex.Message)
End Try
Private Sub TickMe()
CL(0).Actual = H.Actual
TextBoxSerialNumber.Focus()
IntSec = IntSec + 1
H.ActualBoxTime = H.ActualBoxTime.AddSeconds(1)
CL(0).ActualBoxTime = H.ActualBoxTime
If IntSec Mod 60 = 0 Then
H.Actual = H.Actual + 1
CL(0).Actual = H.Actual
End If
CL(0).TargetBox = H.TargetBox
PopulateCutGrid(CL)
End Sub
I’ve found after 50-60 minutes of counting it’s off by almost 10 minutes!
From the MSDN article regarding DispatchTimer:
If you need something more precise, I would recommend System.Threading.Timer. Each time the method of this timer is invoked, a ThreadPool thread is used. From this article:
Also forgot to mention that you’ll need to use
InvokeandInvokeRequiredfor WinForms orDispatcherfor WPF (I think, since I don’t use WPF).Edit 1:
Regarding your comment about needing total seconds, you can just store the original
DateTimesomewhere, and total seconds would be calculated using(DateTime.Now - StoredDateTime).TotalSeconds(). I would also recommend reading this article about threading in WPF to see if it applies in your case.Edit2:
Actually, since I think the only thing you’re after is a precise count of elapsed time, you could use a Stopwatch to keep track of time. Try using the regular
DispatchTimerand just refer to the stopwatch each “tick” of that timer. That way, the Stopwatch is what keeps track of time, not the timer that could be used just for updating the UI.