2010-02-06 120 views
1

我有一個應用程序從Web數據庫讀取一些數據並定期更新本地數據庫。每5分鐘說一次。定時器不在Windows服務應用程序中運行

這是一個交互式窗口應用程序的形式。但是由於這個應用程序必須連續運行,我們決定製作一個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中。但是,即使代碼從未在任何tick函數上打破。

我們不能在服務中使用計時器嗎?如果不是,那麼比另一種方式。如果是的話,這裏可能有什麼問題?

感謝 拉胡爾·哈因

回答

0

沒有什麼阻止計時器在Windows服務工作。這聽起來像定時器沒有配置爲自動重置,但不知道你正在使用什麼計時器(System.Timers.Timer?System.Threading.Timer?Other?)以及它如何配置,這是不可能知道的當然。

關於調試服務,把下面的代碼「綱領性斷點」在你的OnStart()方法:

System.Diagnostics.Debugger.Break(); 

當您啓動該服務,你被提示輸入一個調試會話,並執行將在這條線上休息。你可以從那裏正常調試。

相關問題