在Windows服务中,计时器停止工作

时间:2014-11-26 15:01:01

标签: vb.net system.timers.timer

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

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

它显示已开始但没有任何事情发生。

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

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

0 个答案:

没有答案