为什么这个计时器不能在.net服务应用程序中启动?

kev*_*ley 3 .net vb.net windows-services timer

我有这个代码用于我在.NET中编写的Windows服务....但是无论我在tmrRun属性中放入什么时间间隔,TICK函数都不会被执行.我错过了什么?我确信这是我看不到的愚蠢行为.

谢谢

    
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

Ric*_*dOD 6

如果您使用的是无法在服务中运行的System.Windows.Forms.Timer.查看另外两次可以在.NET中使用的内容.


Jan*_*rup 6

这对我有帮助.

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