views:

458

answers:

5

I have this code for a Windows service I am writing in .NET....However the TICK function never gets executed regardless of what interval I put in the tmrRun properties. What am I missing? I am sure its something stupid I am not seeing.

Thank You

    
Imports System.IO

Public Class HealthMonitor

    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.
        tmrRun.Start()
    End Sub

    Protected Overrides Sub OnStop()
        // Add code here to perform any tear-down necessary to stop your service.
        tmrRun.Stop()
    End Sub

    Private Sub tmrRun_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrRun.Tick
        //DO some stuff
    End Sub
End Class

A: 

Have you attached the eventhandler using AddHandler?

shahkalpesh
+7  A: 

If you are using System.Windows.Forms.Timer that won't work in a service. Look into the other two times you can use in .NET.

RichardOD
A: 

You are using Windows.Form.Time and the Tick event of that, which is not going to work in a Windows Service.

You will have to System.Timers and the Elapsed event to make it work.

Bhaskar
+2  A: 

You need to bind your events properly. You can use System.Timers.Timer and bind the Elapsed event to tmrRun_Tick before you start the timer.

This article explains the different timers.

Jon Freeland
A: 

Add an AddHandler to your OnStart sub.

Eg. :

Dim TimerX as new system.threading.timer
'You can also use
'Dim TimerX as new system.timers.timer
'And you can't use
'Dim TimerX As new system.windows.forms.timer
'As it is only available in forms

public sub onStart(ByVal args as ...)
AddHandler TimerX.Elapsed, AddressOf TimerX_Tick
With TimerX
.interval = 1000
.enabled = true
End With
end sub

private sub TimerX_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Tick
End sub
NicoJuicy