【问题标题】:Timers not running in Windows Service Application计时器未在 Windows 服务应用程序中运行
【发布时间】:2010-02-06 12:12:54
【问题描述】:

我有一个应用程序,它从 Web 数据库中读取一些数据并定期更新本地数据库。每 5 分钟说一次。

这是一个交互式窗口应用程序的形式。但由于这个应用程序必须连续运行,我们决定制作一个 Windows 服务应用程序。

我们在应用程序中使用了计时器,它设置为 5 分钟的间隔,并且在计时器的每个滴答声中,我们都在检查网络更新。

我们在服务中携带了同样的东西。但似乎计时器现在没有运行。以下是服务中的几种方法:

Protected Overrides Sub OnStart(ByVal args() As String)
    ' Add code here to start your service. This method should set things
    ' in motion so your service can do its work.

    'connect to sap business one
    If Connect("rlap1", "sa", "dracula", "UnimaxNew", "manager", "manager") = False Then
        End
        SyncEvents.WriteEntry("Unable to connect to SAP Business One, " & oCompany.GetLastErrorDescription, EventLogEntryType.Error)
    Else
        SyncEvents.WriteEntry("Connected to SAP Business One.", EventLogEntryType.Information)
    End If

    'start the timers
    WebTimer.Start()
    SapTimer.Start()
    DataTimer.Start()

    SyncEvents.WriteEntry("Synchronization process started.")
End Sub
Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
    oCompany.Disconnect()
    SyncEvents.WriteEntry("Disconnected from SAP Business One.", EventLogEntryType.Information)

    'stop the timers
    WebTimer.Stop()
    SapTimer.Stop()
    DataTimer.Stop()

    SyncEvents.WriteEntry("Synchronization process stopped.")
End Sub
Private Sub WebTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebTimer.Tick
    SyncEvents.WriteEntry("Checking for new orders.")
    CheckOrder()
End Sub
Private Sub SapTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SapTimer.Tick
    SyncEvents.WriteEntry("Checking for new deliveries.")
    CheckDelivery()
End Sub
Private Sub DataTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataTimer.Tick
    SyncEvents.WriteEntry("Checking for stock updates.")
    CheckStock()
End Sub

消息正确记录在事件日志中 - 当我们启动或停止服务时。但是没有来自计时器功能的消息。我也尝试过调试服务——在 Visual Studio 中使用 Attach To Process..。但即使代码从未在任何刻度函数上中断。

我们不能在服务中使用计时器吗?如果不是,那另一种方式是什么。如果是,这里可能有什么问题?

谢谢 拉胡尔耆那教

【问题讨论】:

    标签: windows-services timer


    【解决方案1】:

    没有什么可以阻止计时器在 Windows 服务中工作。在我看来,定时器未配置为自动重置,但不知道您使用的是什么定时器(System.Timers.Timer?System.Threading.Timer?其他?)以及它是如何配置的,不可能知道当然。

    关于调试服务,在您的 OnStart() 方法中放置以下代码“程序断点”:

    System.Diagnostics.Debugger.Break();
    

    当您启动服务时,系统会提示您进入调试会话,执行将在此行中断。您可以从那里正常调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      相关资源
      最近更新 更多