这是处理我服务的类:
Imports System.Timers
Imports System.Configuration
Public Class Service
Private _lastRun As DateTime = DateTime.Now
Private ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
Protected Overrides Sub OnStart(ByVal args() As String)
log.Info("Service started.")
Dim timer As New System.Timers.Timer(1 * 60 * 1000)
AddHandler timer.Elapsed, AddressOf timer_Elapsed
timer.Start()
End Sub
Private Sub timer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs)
Dim now As DateTime = DateTime.Now
If now.Minute > _lastRun.Minute Then
Dim m As New Main()
m.Main()
m = Nothing
End If
_lastRun = now
End Sub
Protected Overrides Sub OnStop()
log.Info("Service is stopped")
End Sub
End Class
一分钟后执行Main
方法。 Main
方法的运行时间是不可预测的。如果执行时间超过一分钟,则执行另一个Main
方法。我会禁止这种行为,因此如果Main
方法仍在运行,则必须禁止Main
方法的新实例。我能做什么?
答案 0 :(得分:1)
定义您的计时器对象,使其具有该类的范围:
private _timer As New System.Timers.Timer(1 * 60 * 1000)
然后当计时器结束时,将其禁用,直到完成主程序:
Private Sub timer_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs)
_timer.Enabled = False
Dim now As DateTime = DateTime.Now
If now.Minute > _lastRun.Minute Then
Dim m As New Main()
m.Main()
m = Nothing
End If
_lastRun = now
_timer.Enabled = True
End Sub