Jump to content
Xtreme .Net Talk

Windows Service Threading Timer stops working after a while


Recommended Posts

Posted

Hi all,

 

I've got a Windows Service that simple writes a record to a database whenever the callback of the timer is triggered (see code below). The strange thing is that after a while the timer callback isn't called anymore (I usually get 10 records in the database). Any ideas?

 

Public Class Service1

#Region " Own Functions And Procedures "
   Private Function Insert_Event(ByVal pEvent As String, ByVal pChanged As DateTime) As Boolean
       Try
           Dim db As New dsAutoServiceTableAdapters.sp_event_insertTableAdapter

           db.GetData(pEvent, pChanged)

           db = Nothing

           Return True
       Catch ex As Exception
           Dim strError As String = ex.ToString

           Return False
       End Try
   End Function
#End Region

   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.

       System.IO.File.AppendAllText("C:\AutoServiceLog.txt", "[" & Now.ToString & "] Service successfully started." & vbNewLine)

       Const intInterval As Integer = 30000 '30 seconds.
       Dim tAutoService As System.Threading.Timer
       Dim tDelegate As System.Threading.TimerCallback = AddressOf EventAction

       tAutoService = New System.Threading.Timer(tDelegate, Me, 0, intInterval)
   End Sub

   Protected Overrides Sub OnStop()
       ' Add code here to perform any tear-down necessary to stop your service.
       System.IO.File.AppendAllText("C:\AutoServiceLog.txt", "[" & Now.ToString & "] Service successfully stopped." & vbNewLine)
   End Sub

   Public Sub EventAction(ByVal sender As Object)
       If Insert_Event("EventAction triggered", Now) = False Then
           System.IO.File.AppendAllText("C:\AutoServiceLog.txt", "[" & Now.ToString & "] Service generated error." & vbNewLine)
       End If
   End Sub
End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...