为什么我的asp.net mvc计时器停止了?

时间:2016-05-26 19:54:08

标签: asp.net asp.net-mvc vb.net timer

我创建了一个asp.net mvc应用程序,每隔10分钟读取特定用户生成的推文。我通过计时器完成这项任务。计时器完美地运行2到5次,然后计时器停止,没有异常或错误。

有人知道为什么会发生这种情况吗?如何将其设置为始终运行直到我停止计时器???

这里是计时器配置和计时器的处理程序:

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

1 个答案:

答案 0 :(得分:2)

要做到这一点并不容易。幸运的是,一些疯狂的智能开发者已经为你做了很多困难。

查看Hangfire它允许您安全地安排在Asp.net应用程序中按计划运行的任务。