Why does this timer not start in a .net service application?

I have this code for a Windows service that I write in .NET ... However, the TICK function never runs regardless of the interval I entered in the tmrRun properties. What am I missing? I’m sure that something stupid is something I don’t see.

thanks

    
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

+3
source share
7 answers

If you use System.Windows.Forms.Timer, which will not work in the service. Look at the other two times you can use in .NET.

+6
source

It helps me.

Imports system
Imports System.Timers

Public Class MyTimers

    Protected Overrides Sub OnStart (ByVal args () As String)

        Dim MyTimer As New System.Timers.Timer()
        AddHandler MyTimer.Elapsed, AddressOf OnTimedEvent

        ' Set the Interval to 10 seconds (10000 milliseconds).
        MyTimer.Interval = 10000
        MyTimer.Enabled = True
    End Sub

    Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("Hello World!")
    End Sub
End Class
+6

. System.Timers.Timer Elapsed tmrRun_Tick, .

.

+3

AddHandler?

0

Windows.Form.Time Tick, Windows.

System.Timers , .

0

AddHandler OnStart.

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
0

, System.Timers.Timer, Elapsed... , .

, , /, .

Windows (System.Diagnostic.EventLog), , . .

0

Source: https://habr.com/ru/post/1710290/


All Articles