Timers.timer lossing time when I compared it to the system time

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span lang="EN-ZA Hi
<span lang="EN-ZA Please help.
<span lang="EN-ZA When I run my app at first its keeps time accurately for the first minute or so there after it starts loosing seconds, and by the end of the day its minutes behind.
<span lang="EN-ZA <img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/81572
<span lang="EN-ZA I’ve created a class called ServerTimeTimer that has a timers.timer
<span> object that elapses every second and adds a second to a dateTime variable, and a property to retrieve the date time. See class code below.
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt Public<span style="font-family:Courier New; font-size:10pt
<span style="color:blue Class ServerTimeTimer
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private <span style="color:blue WithEvents serverTimer
<span style="color:blue As <span style="color:blue New Timers.Timer
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private my_ServerDateTime <span style="color:blue
As <span style="color:blue New DateTime
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Public <span style="color:blue ReadOnly
<span style="color:blue Property GetServerDateTime() <span style="color:blue
As DateTime
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Get
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Return my_ServerDateTime
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Get
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Property
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Public <span style="color:blue Sub
<span style="color:blue New()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:green my_ServerDateTime = ServerTime
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Public <span style="color:blue Sub StartServerTimeTimer(<span style="color:blue ByVal serverDateTime
<span style="color:blue As DateTime)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
my_ServerDateTime = serverDateTime
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
serverTimer.Interval = 1000 - Now.Millisecond
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
serverTimer.Start()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Public <span style="color:blue Sub StopServerTimeTimer()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
serverTimer.Stop()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private <span style="color:blue Sub ServerTimer_Elapsed(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.Timers.ElapsedEventArgs)
<span style="color:blue Handles serverTimer.Elapsed
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
serverTimer.Stop()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Dim interval <span style="color:blue As
<span style="color:blue Integer = 1000 - Now.Millisecond
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
my_ServerDateTime = my_ServerDateTime.AddMilliseconds(interval)
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
serverTimer.Interval = interval
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
serverTimer.Start()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub
<span style="line-height:115%; font-family:Courier New; color:blue; font-size:10pt End<span style="line-height:115%; font-family:Courier New; font-size:10pt
<span style="color:blue Class
<span style="line-height:115%; font-family:Courier New; color:blue; font-size:10pt
<span lang="EN-ZA I also have a form that has 2 labels, one to display the server time and one that display’s the local system time and a forms.timer object to update the form every 500 milliseconds with the server time and the local system time. When I
call the StartServerTimeTimer procedure I pass it the local system date and time + 2 hours to simulate a different time.
<span lang="EN-ZA After running the application for a while, there is a difference bigger than 2 hours between the local system time and the time kept by the class.
<span lang="EN-ZA Why is that and how do I keep an accurate count so that by the end of the day the difference between the two times is still 2 hours?
<span> See Form Code Below.
<span lang="EN-ZA
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt Public<span style="font-family:Courier New; font-size:10pt
<span style="color:blue Class Form1
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private my_ServerTimer <span style="color:blue
As <span style="color:blue New ServerTimeTimer
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private <span style="color:blue Sub Form1_Load(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue
Handles <span style="color:blue Me.Load
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
my_ServerTimer.StartServerTimeTimer(Now.AddHours(2))
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
Timer1.Interval = 500
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span> Timer1.Start()
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; color:blue; font-size:10pt
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue Private <span style="color:blue Sub Timer1_Tick(<span style="color:blue ByVal sender
<span style="color:blue As <span style="color:blue Object, <span style="color:blue
ByVal e <span style="color:blue As System.EventArgs) <span style="color:blue
Handles Timer1.Tick
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
Label1.Text = <span style="color:#a31515 "Server DataTime: " & my_ServerTimer.GetServerDateTime
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
Label2.Text = <span style="color:#a31515 "Local System DataTime: " & Now
<p style="line-height:normal; margin-bottom:0pt <span style="font-family:Courier New; font-size:10pt <span>
<span style="color:blue End <span style="color:blue Sub
<span style="line-height:115%; font-family:Courier New; color:blue; font-size:10pt End<span style="line-height:115%; font-family:Courier New; font-size:10pt
<span style="color:blue Class

View the full article
 
Back
Top