我有一个Windows服务,如下图所示:
Protected Overrides Sub OnStart(ByVal args() As String)
Dim timer As System.Timers.Timer = New System.Timers.Timer()
timer.Interval = 1000 '1 sec
AddHandler timer.Elapsed, AddressOf Me.OnTimer
timer.Start()
End Sub
Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
Dim Time As String = DateTime.Now.ToString("hh:mm:ss tt")
If Time = "01:58:15 PM" Or Time = "02:42:15 PM" Or Time = "02:43:15 PM" _
Or Time = "03:44:15 PM" Or Time = "06:00:15 PM" _
Or Time = "12:00:15 AM" Or Time = "05:00:15 AM" Then
Dim path As String = Application.StartupPath & "\test.log"
Dim strFile As String = String.Format(path, _
DateTime.Today.ToString("dd-MMM-yyyy"))
File.AppendAllText(strFile, String.Format("Message at-- {0}{1}", _
DateTime.Now, Environment.NewLine))
End If
End Sub
你能向我解释为什么我有两个输入下午3点44分15秒? 我想计时器事件的OnTimer被搁置,直到前面的计时器触发作业完成,然后时间会有所不同,它不会同一时间内犹豫。 怎么了?
Message at-- 1/13/2015 3:44:15 PM
Message at-- 1/13/2015 3:44:15 PM
Message at-- 1/14/2015 12:00:15 AM
Message at-- 1/14/2015 5:00:15 AM
对于进一步的讨论:
Private Sub OnTimer(sender As Object, e As Timers.ElapsedEventArgs)
RemoveHandler timer.Elapsed, AddressOf Me.OnTimer
Dim Time As String = DateTime.Now.ToString("hh:mm:ss tt")
If Time = "01:58:15 PM" Or Time = "02:42:15 PM" Or Time = "02:43:15 PM" _
Or Time = "03:44:15 PM" Or Time = "06:00:15 PM" _
Or Time = "12:00:15 AM" Or Time = "05:00:15 AM" Then
Dim path As String = Application.StartupPath & "\test.log"
Dim strFile As String = String.Format(path, _
DateTime.Today.ToString("dd-MMM-yyyy"))
File.AppendAllText(strFile, String.Format("Message at-- {0}{1}", _
DateTime.Now, Environment.NewLine))
End If
AddHandler timer.Elapsed, AddressOf Me.OnTimer
End Sub