Henry
Henry

Reputation: 317

Why Does my asp.net mvc timer stop?

I've created an asp.net mvc application that reads every 10 minutes the tweets generated by specific users. I make this task trough a timer. The timer runs perfectly between 2 and 5 times and then the timer stops without an exception or an error.

Does someone know why this happen and how can I set it to always run until I stop the timer???

Here the timer configuration and the timer's handler:

Public Shared Function Start()
    Dim db As New EnubexDbContext
    Dim provider As CultureInfo = CultureInfo.InvariantCulture
    Dim tiempo As Double = Convert.ToDouble(db.ParametrosGenerales.Where(Function(pg) pg.sDescripcionParametroGeneral = "frecuenciaSeekerTwitterTimer").Select(Function(pg) pg.sValorParametroGeneral).FirstOrDefault())
    If Not IsNothing(tiempo) Then
        timer = New Timer(tiempo)
        If Not IsNothing(timer) Then
            ApplicationLog.EscribirActividad("Se creo el objeto timer")
        End If
        Try
            AddHandler timer.Elapsed, New ElapsedEventHandler(AddressOf Handler)
            ApplicationLog.EscribirActividad("Se agregó el handler")
            timer.Enabled = True
            If Not IsNothing(timer) Then
                ApplicationLog.EscribirActividad("Se activó el timer")
            End If
            Return (0)
        Catch ex As Exception
            ApplicationLog.EscribirExcepctionLog(ex)
            Return (1)
        End Try
    Else
        Return (1)
    End If
End Function

Shared Async Sub Handler(ByVal sender As Object, ByVal e As ElapsedEventArgs)
    ApplicationLog.EscribirActividad("Empezando búsqueda")
    Try
        Await SeekTwitter()
        ApplicationLog.EscribirActividad("Terminando búsqueda")
    Catch ex As Exception
        ApplicationLog.EscribirExcepctionLog(ex)
    End Try
End Sub

Upvotes: 0

Views: 367

Answers (1)

etoisarobot
etoisarobot

Reputation: 7794

To do this right is not easy. Luckily some crazy smart dev has already done the hard part for you.

Take a look at Hangfire It allows you to safely schedule tasks to run on a schedule in an Asp.net app.

Upvotes: 2

Related Questions