Hi,

I need help regarding the timer. I’m developing a SMTP email program that will send out email periodically. My program has a webpage where user can start/stop timer to send out email. Once the user press on the ‘start timer button’ on webpage, my program will call the timer.vb class (this class will start the timer and handle elapsed event). Code as below:

Private Sub btn_timer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_timer.Click

btn_timer.Enabled = False
timer1.startTimer(txt_interval.Text, txt_to.Text)

End Sub

My problem is I can’t stop the timer. Even after setting the timer.enabled = false, the timer is still running. I even tried putting the timer inside the aspx.vb file (instead of calling another class) but it is not working also.

Private Sub btn_stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_stop.Click
btn_timer.Enabled = True
timer1.stopTimer()
End Sub

I don’t know what goes wrong as this method works well in windows form. Would someone please guide me? Below are the codes for timer.vb: -

Public Class timer
Private emailadd As String = Nothing
Private flag As Boolean = True
Dim Timer1 As New System.Timers.Timer()

Public Sub startTimer(ByVal interval As Integer, ByVal email As String)
Timer1.Interval = interval
Timer1.Enabled = True
emailadd = email
AddHandler Timer1.Elapsed, AddressOf Timer1_Elapsed
End Sub

Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) 'Handles Timer1.Elapsed
Dim mail As New mailAdapter()
mail.newMail(emailadd, "[email protected]")
End Sub

Public Sub stopTimer()
Timer1.Enabled = False
End Sub

End Class


Thanks!