System.Timer.Timers on aspx page: How to stop timer
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!
;)
Re: System.Timer.Timers on aspx page: How to stop timer
Try setting you Timer1.Interval=0. It is the same as disabling your timer.
If it does not work then the timer did not went to the event.
Try to establish breakpoints so that you can trace where the process is going through.
Re: System.Timer.Timers on aspx page: How to stop timer
It's a web application. No timer. Don't use it.
You should be using a windows service for this task if you want to use a timer.