日期:2014-05-17  浏览次数:20761 次

Timer 定时器的问题------50分
尝试做一个windows下定时访问数据的服务。

正式工作可能一天运行一次。
为了测试设置了时间间隔为一分钟,timer elsped时间触发后也就是向一个文本文件里写一下当前时间。

现在发现有个问题就是,这个时间逐渐在变,并不是间隔的很准

2012-3-26 10:32:54------2012-3-26 10:32:54
2012-3-26 10:33:55------2012-3-26 10:33:55
2012-3-26 10:34:55------2012-3-26 10:34:55
2012-3-26 10:35:55------2012-3-26 10:35:55
2012-3-26 10:36:56------2012-3-26 10:36:56
2012-3-26 10:37:56------2012-3-26 10:37:56
2012-3-26 10:38:56------2012-3-26 10:38:56
2012-3-26 10:39:56------2012-3-26 10:39:56
2012-3-26 10:31:54------2012-3-26 10:31:54
2012-3-26 10:32:54------2012-3-26 10:32:54
2012-3-26 10:33:54------2012-3-26 10:33:54
2012-3-26 10:34:54------2012-3-26 10:34:54
2012-3-26 10:35:54------2012-3-26 10:35:54
2012-3-26 10:36:54------2012-3-26 10:36:54
2012-3-26 10:37:54------2012-3-26 10:37:54
2012-3-26 10:38:53------2012-3-26 10:38:53
2012-3-26 10:39:53------2012-3-26 10:39:53
2012-3-26 10:40:53------2012-3-26 10:40:53
2012-3-26 10:41:53------2012-3-26 10:41:53
2012-3-26 10:42:53------2012-3-26 10:42:53
2012-3-26 10:43:53------2012-3-26 10:43:53
2012-3-26 10:44:52------2012-3-26 10:44:52
2012-3-26 10:45:52------2012-3-26 10:45:52
2012-3-26 10:46:52------2012-3-26 10:46:52
2012-3-26 10:47:52------2012-3-26 10:47:52
2012-3-26 10:48:52------2012-3-26 10:48:52
2012-3-26 10:49:52------2012-3-26 10:49:52
2012-3-26 10:50:52------2012-3-26 10:50:52
2012-3-26 10:51:51------2012-3-26 10:51:51
2012-3-26 10:52:51------2012-3-26 10:52:51
2012-3-26 10:53:51------2012-3-26 10:53:51
2012-3-26 10:54:51------2012-3-26 10:54:51
2012-3-26 10:55:51------2012-3-26 10:55:51
2012-3-26 10:56:51------2012-3-26 10:56:51
2012-3-26 10:57:51------2012-3-26 10:57:51
2012-3-26 10:58:50------2012-3-26 10:58:50



之后改成时间间隔为一个小时,发现也有问题,偏差更大

2012-3-26 12:33:24------2012-3-26 12:33:24
2012-3-26 13:37:08------2012-3-26 13:37:08
2012-3-26 14:29:34------2012-3-26 14:29:34

如上,开始还有些反复,后来就比较有规律的越来越短了。不知道这是因为什么?
其实我的这个应用对时间要求没有这么的高,但只是想弄明白,如果真有对时间要求必须控制在一秒左右的怎么解决?
以下是代码


Public Class Service1

  Public Shared Function Validatordate(ByVal strDateA As String, ByVal strDateB As String) As Double
  Dim ts As TimeSpan = Convert.ToDateTime(strDateB) - Convert.ToDateTime(strDateA)
  Dim count As Double = ts.TotalSeconds * 1000
  Return count
  End Function

  Protected Overrides Sub OnStart(ByVal args() As String)
   
  writeTest(System.Configuration.ConfigurationManager.AppSettings("serviceLog").ToString(), "Service is started")
  Dim sleeptime As Double = Validatordate(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), System.DateTime.Now.ToString("yyyy-MM-dd ") + Configuration.ConfigurationManager.AppSettings("runTime").ToString)
  'Dim sleeptime As Double = Validatordate(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), System.DateTime.Now.ToString("yyyy-MM-dd ") + "10:15")


  If sleeptime < 0 Then
  sleeptime += 24 * 60 * 60 * 1000
  End If

  Dim t As System.Timers.Timer = New System.Timers.Timer(sleeptime)
  AddHandler t.Elapsed, AddressOf OnTimeEvent
  t.Interval = 1000 * 60 * 60
  t.AutoReset = True
  t.Enabled = True
  writeTest("c://service.log", "runend")

  End Sub

  Protected Overrides Sub OnStop()
  ' Add code here to perform any tear-down necessary to stop your service.
  wr