How to fire timer.Elapsed event immediately

前端 未结 10 1232
独厮守ぢ
独厮守ぢ 2020-12-10 00:17

I\'m using the System.Timers.Timer class to create a timer with an Timer.Elapsed event. The thing is the Timer.Elapsed event is fired

10条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 00:53

    I have implemented in VB.NET with AddHandler

    Public Class clsMain Inherits ServiceBase
    
    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.
    gTimer.Interval = 1000  
    gTimer.Enabled = True
    AddHandler gTimer.Elapsed, AddressOf gTimer_Elapsed
    End Sub
    
    'When the timer is elapsed this event will be fired
    
    Protected Sub gtimer_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs)
    'Stop the timer and do some work
    gTimer.Stop()
    'Custom code
    'Here
    'Start to raise the elapsed event again
    gTimer.Start()
    End Sub
    End Class
    

提交回复
热议问题