Reputation: 20474
I'm getting problems trying to show the remainig time of a operation
The problem is only with the remaining timer, I get a negative values...
This is a example of a operation of 10000 ms (10 secs):
So anyway if i remove the "-" char from the timespan the hour and the minute stills incorrects values...
Dim time_out as integer = 60000 ' 'Milisegundos
Dim StartTime As DateTime ' Tiempo inicio
Dim EndTime As DateTime ' Tiempo final
Dim ElapsedTime As TimeSpan ' Tiempo transcurrido
Dim RemainingTime As TimeSpan ' Tiempo restante
' Elapsed Time
#Region " Elapsed Time Function "
Public Function Print_Elapsed_Time()
If StartTime.ToString = "01/01/0001 0:00:00" Then
StartTime = Now
StartTime = StartTime.AddSeconds(-1)
End If
ElapsedTime = Now().Subtract(StartTime)
Return String.Format("{0:00}:{1:00}:{2:00}", CInt(Math.Floor(ElapsedTime.TotalHours)) Mod 60, CInt(Math.Floor(ElapsedTime.TotalMinutes)) Mod 60, CInt(Math.Floor(ElapsedTime.TotalSeconds)) Mod 60)
End Function
#End Region
#Region " Remaining Time Function "
Public Function Print_Remaining_Time()
If EndTime.ToString = "01/01/0001 0:00:00" Then
EndTime = Now
EndTime = EndTime.AddMilliseconds(Time_Out - 1000)
End If
RemainingTime = Now().Subtract(EndTime)
Return String.Format("{0:00}:{1:00}:{2:00}", CInt(Math.Floor(RemainingTime.TotalHours)) Mod 60, CInt(Math.Floor(RemainingTime.TotalMinutes)) Mod 60, CInt(Math.Floor(RemainingTime.TotalSeconds)) Mod 60).Replace("-", "")
End Function
#End Region
Upvotes: 0
Views: 4884
Reputation: 31443
I think for the End Time you want :
RemainingTime = EndTime.Subtract(Now)
Otherwise, I'm not sure how you initialized these, but I made this change :
Dim StartTime As DateTime = DateTime.MinValue ' Tiempo inicio
Dim EndTime As DateTime = DateTime.MinValue ' Tiempo final
and also :
If StartTime = DateTime.MinValue Then ' ... etc '
but perhaps a flag or something is altogether a better way to signal a reset.
Upvotes: 1
Reputation: 11773
Here is an example of a countdown from 10 seconds using a timer and two labels
'example - countdown from 10 secs
Dim countdown As New TimeSpan(0, 0, 10)
Dim stpw As New Stopwatch
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If stpw.IsRunning Then
stpw.Stop()
Timer1.Stop()
Else
stpw.Stop()
stpw.Reset()
stpw.Start()
Timer1.Interval = 100
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'label1 - elapsed time
'label2 - time remaining
If stpw.Elapsed <= countdown Then
Label1.Text = stpw.Elapsed.ToString
Label2.Text = (countdown - stpw.Elapsed).ToString
Else
stpw.Stop()
Label1.Text = countdown.ToString
Label2.Text = "00:00:00"
End If
End Sub
Upvotes: 2