Click here to Skip to main content
15,900,818 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created a countdown timer displaying the time as 'hh:mm:ss', this shows the remaining time, but instead of the second decreasing by 1, it decreases by 2? The interval of timer is set to 1000. Here is my handler for Tick event:
VB
Private Sub Tick_Handler(ByVal sender As Object, ByVal e As EventArgs) Handles t.Tick
ts -= 1
RemainingTime.Text = ts\3600 & ":" & (ts Mod 3600)\60 & ":" & ts Mod 60
End Sub

ts is the amount of time in miliseconds.
I can't understand how ts is decreased by 2 instead of 1?
Could you help me explain that strange thing?
Your help would be highly appreciated!
Posted
Updated 26-Dec-11 9:01am
v2

It can be somewhere else. Just check where else you use the member ts. But better don't — the whole thing makes no sense at all. You are trying to count what you think is milliseconds in the cycle of what is supposed to be a second. And also, why do you think the timer is accurate enough. You don't need this member at all. You can always get current time using System.DateTime.Now. To count remaining time, remember some time and calculate remaining time by subtract operator defined for the type System.DateTime.

You also don't need to convert time into minutes, seconds, etc. — it is already done for you. You can get any units you need, either in fractional form (double floating-point type) or in whole integer. And if this is just for showing value on screen, use System.DateTime.ToString with appropriate format specifier.

See:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

—SA
 
Share this answer
 
Comments
supernorb 27-Dec-11 0:21am    
I'm sorry, I have found this is for my adding twice the handler to tick event. So it is decreased by 1 twice nearly simultaneously making me think it is decreased by 2.
Thank you!
Sergey Alexandrovich Kryukov 27-Dec-11 0:27am    
I knew that. However, you don't need to count; better use my advice.
Good luck,
--SA
Try similar thread:
Countdown timer
 
Share this answer
 
Actually the code you posted works fine, at least on my system. Please check if there are bugs in other parts of your application.
 
Share this answer
 
Comments
supernorb 27-Dec-11 0:23am    
You are right, it is my fault. Please see my comment to SA's solution.
Thanks!

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