计时器在asp.net中每2分钟执行两次代码



我有一个按钮,当它点击它将启动一个存储过程来执行sql作业。在按钮中,我有一个方法。在该方法中,我希望代码等待2分钟并运行sql查询以获取状态,如果状态= 4或状态= 7则停止,但如果状态不是4或7,则相同的代码在两分钟内再次运行,但只有两次。我如何在vb.net网站实现这一点?下面是我到目前为止尝试过的,但我得到了错误,我以前从未做过一次。

   Dim myTimer As New System.Timers.Timer(2 * 60 * 1000)
   Private Sub DisableButton()
      btnBertFish.Enabled = False
      myTimer.Elapsed = New ElapsedEventHandler(AddressOf (OnTimedEvent()))
   End Sub
   Private Sub OnTimedEvent()
   'Some code that need to execute
   End Sub

所以,到目前为止,这些是我得到的错误:公共事件流逝(发送方作为对象,e作为System.Timers.ElapsedEventArgs)是一个事件,不能直接调用。使用'RaiseEvent'语句引发事件。

我看了教程和系统。计时器,但我不知道怎么开始。有人能帮帮我或给我指个方向吗?

 Dim myTimer As New System.Timers.Timer()
 myTimer .AutoReset = True
 myTimer .Interval = 60 * 1000 '1 minute
 AddHandler myTimer.Elapsed, AddressOf OnTimedEvent
 dim i as integer=0
 Private Sub DisableButton()
    btnBertFish.Enabled = False
    myTimer.Elapsed = New ElapsedEventHandler(AddressOf (OnTimedEvent()))
 End Sub   
 Private Sub OnTimedEvent()
 i += 1
 if i= 2 then
 ' Run your code
 'i=2 means the second tick of the timer since i value is increased in each 1 minute
 End IF
 End Sub

在需要延迟的情况下添加myTimer.Start(),以停止计时器或重置添加计时器块代码,但我不清楚thestatus是什么:

 if theStatus = 4 or theStatus = 7 then
 myTimer.Stop()
 else
 i=0
 End If

最新更新