asp.net app中的计时器问题

时间:2011-09-09 19:03:11

标签: .net vb.net

当用户点击“授权”按钮时,我必须每2秒检查一次数据库的值。如果我得到一个值,我必须显示一个面板进行处理,否则我必须向用户显示其他消息。

我做的方式不起作用。定时器停止后,不清楚代码的位置。

我的代码如下:

Private aTimer As System.Timers.Timer
Dim AuthID As Integer = 0
Dim counter As Integer = 0
Private thisLock As New Object
Private AuthCode As String = "Nothing"
Protected Sub btnAuthorize_Click(ByVal sender As Object, ByVal e As EventArgs) Handles    btnAuthorize.Click
    If ValidateForm() Then
        Dim dr As DataRow = Nothing
        dr = DataAccess.SaveAuthorization(txtAccuntNumber.Text, lblAmountFinance.Text)
        If dr("AuthorizationID") <> 0 Then
            AuthID = dr("AuthorizationID")
            dr = Nothing
            aTimer = New System.Timers.Timer(2000)
            aTimer.Start()
            AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        End If
'Code never goes to the lines below
        If AuthCode <> "" Then
            lblauthorizationAmmount.Text = lblAmountFinance.Text
            lblAuthorizationNumber.Text = AuthCode
            panelAuthorize.Visible = True
        End If

    End If
End Sub
Public Sub OnTimedEvent()
    SyncLock (thisLock)
        Dim dr As DataRow = Nothing
        dr = DataAccess.GetAuthorizationConfirmation(AuthID)
        AuthCode = dr("AuthCode")

        counter = counter + 1
        If counter > 1 Or AuthCode <> "" Then

            aTimer.Enabled = False
            aTimer.Stop()
            aTimer.Dispose()

        End If
    End SyncLock
End Sub

0 个答案:

没有答案