0

我有一个每分钟运行一次并执行一些 DB 和 IO 操作的 Windows 服务。

一切正常,但我观察到一段时间后服务不起作用。

它显示已开始,但没有任何反应。

1)这可以是GC收集的计时器吗?如果是,那么我的代码有什么问题。

2) 我在onstart 和onstop 中使用了timer.enable,我应该使用timer.start 和timer.stop。

3)如果我想使用 Threading.Timer 那么我需要在以下代码中进行哪些更改。

任何帮助将不胜感激

Imports System.ServiceProcess
Imports System.Timers
Imports System.Configuration

Public Class ReportingImmediate
    Inherits ServiceBase

#Region "Members"

    Private ReadOnly time As Timers.Timer
    Private ReadOnly ReportingHelper As ReportingHelper
    Private ReadOnly timeInterval As String

#End Region

    Public Sub New()
        ' Initialize Logs
        InitializeComponent()
        ' Initialize other components
        time = New System.Timers.Timer()
        timeInterval = ConfigurationManager.AppSettings("ImmediateReRunInterval")
        time.Interval = Integer.Parse(timeInterval)
        AddHandler time.Elapsed, AddressOf TimeElapsed
        objReportingHelper = New ReportingHelper()

    End Sub

#Region "Timer Event"

    ''' <summary>time Elapsed</summary>
    ''' <param name="sender">The object that raised the event sender</param>
    ''' <param name="e">Event data passed to the handler e</param>
    Private Sub TimeElapsed(sender As Object, e As ElapsedEventArgs)
        time.Enabled = False
        objReportingHelper.GetReportsForExecutions(1)
        ' Enable the Timer
        time.Enabled = True
        time.Interval = Integer.Parse(timeInterval)
    End Sub

#End Region

#Region "Service Events"

    ''' <summary>On Start</summary>
    ''' <param name="args">Arguments</param>
    Protected Overrides Sub OnStart(args As String())
        time.Enabled = True
    End Sub

    ''' <summary>On Stop</summary>
    Protected Overrides Sub OnStop()
        time.Enabled = False
    End Sub

#End Region

End Class
4

0 回答 0