My timer in VB.NET is Losing one second

空扰寡人 提交于 2019-12-13 23:35:03

问题


I use a DateTimePicker to pick a dateTime value and see how much time I have until it is zero:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

timeInSeconds = (DateTimePicker1.Value.Subtract(System.DateTime.Now)).ToString
                'RichTextBox1.Text = RichTextBox1.Text + vbTab + timeInSeconds.ToString
                timeInSeconds = timeInSeconds.Substring(0, timeInSeconds.LastIndexOf("."))

End Sub

The RichTextbox above shows that I loose a second at about each 30 seconds:

23:59:47.4060217
23:59:46.3939638
23:59:45.3799058
23:59:44.3648477
23:59:43.3517898
23:59:42.3377318
23:59:41.3236738
23:59:40.3096158
23:59:39.2955578
23:59:38.2814998
23:59:37.2674418
23:59:36.2533838
23:59:35.2393258
23:59:34.2242677
23:59:33.2112098
23:59:32.1981518
23:59:31.1830938
23:59:30.1690358
23:59:29.1549778
23:59:28.1409198
23:59:27.1268618
23:59:26.1128038
23:59:25.0997458
23:59:24.0856878
23:59:23.0716298
23:59:22.0575718
23:59:21.0435138
23:59:20.0284558
23:59:19.0143978
**23:59:18.0013398
23:59:16.9872818**

so using

timeInSeconds = timeInSeconds.Substring(0, timeInSeconds.LastIndexOf("."))

it goes from 23:59:18 to 23:59:16

Why is that so? What I do wrong? Is it the time that

(DateTimePicker1.Value.Subtract(System.DateTime.Now)).ToString

takes place that I am loosing?

The timer is set to make changes each one second.

What Can I do to have the right results?

Thanks in advance!


回答1:


The accuracy changes based on what you are doing and everything else that is going on at the same time. You can correct for it by resetting the interval every time. Now this breaks down when the task you are doing is longer than a second, but it will keep the ticks happening as close to the second as possible.

   Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
     Timer1.Enabled = False

     'do your stuff here

     Timer1.Interval = 1000 - Date.Now.Millisecond
     Timer1.Enabled = True

  End Sub

I typically use this technique when I want something to happen every minute or every hour on the hour.




回答2:


Try not appending the text directly, but rather concatenate the result in a tmp string. After that assign tmp to textbox1.text.

I've been in a situations where I must modify a textbox every forloop iteration to display one byte in hex, the result was impressive when I shift to the previous methodology.

Give it a try.

From comment:

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
  Dim timeInSeconds = (DateTimePicker1.Value.Subtract(System.DateTime.Now)).ToString 
  'RichTextBox1.Text = RichTextBox1.Text + vbTab + timeInSeconds.ToString
  timeInSeconds = timeInSeconds.Substring(0, timeInSeconds.LastIndexOf("."))
  TextBox1.Text &= timeInSeconds & vbNewLine
End Sub

I tested this code in a 1000 milisec timer and there's no problem.



来源:https://stackoverflow.com/questions/10470276/my-timer-in-vb-net-is-losing-one-second

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!