AFAIK, what’s likely making it feel “jerky” isn’t really the string formatting, but rather the combination of System.Timers.Timer firing on a thread-pool thread, marshaling back to the UI thread every tick with Dispatcher.Invoke, and relying on AutoReset timing, which isn’t very precise under load
Generally, for UI timers in WPF, the recommended approach is to use a DispatcherTimer, which runs directly on the UI thread and integrates with the message pump. In addition, instead of decrementing a counter, you can base the display on actual elapsed time so small delays don’t accumulate.
Private tmr As DispatcherTimer
Private endTime As DateTime
Private Sub StartClock(sender As Object, e As RoutedEventArgs) Handles btnStart.Click
Dim startPeriodSeconds As Integer = 3600 ' for example
endTime = DateTime.Now.AddSeconds(startPeriodSeconds)
tmr = New DispatcherTimer()
tmr.Interval = TimeSpan.FromSeconds(1)
AddHandler tmr.Tick, AddressOf UpdateClock
tmr.Start()
End Sub
Private Sub UpdateClock(sender As Object, e As EventArgs)
Dim remaining As TimeSpan = endTime - DateTime.Now
If remaining.TotalSeconds > 0 Then
txtRemaining.Text = remaining.ToString("hh\:mm\:ss")
Else
txtRemaining.Text = "00:00:00"
tmr.Stop()
End If
End Sub
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin