【问题标题】:Why Does my asp.net mvc timer stop?为什么我的 asp.net mvc 计时器停止?
【发布时间】:2016-05-27 05:13:37
【问题描述】:

我创建了一个 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

【问题讨论】:

  • 这可能是因为AppPool正在回收,破坏了定时器。从哪里调用Start()
  • 不要自己在 ASP.NET 中运行后台进程。你会搞砸的。 Scott Hanselman 在他的博客 here 上讨论了这一点。

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


【解决方案1】:

要做到这一点并不容易。幸运的是,一些疯狂的聪明开发者已经为你完成了困难的部分。

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

【讨论】:

  • 您好,感谢您的回答。我正在我的项目中尝试hangfire,使用这行代码来创建一个经常性的工作-> RecurringJob.AddOrUpdate(Of SeekerTwitterTimer)(Function() SeekerTwitterTimer.SeekTwitter, "*/15 * * * *") 但我正在执行应用程序我收到以下错误:“表达式主体的类型应为MethodCallExpression 参数名称:methodCall”。你知道发生了什么吗?
  • @Henry 也许试试这个 RecurringJob.AddOrUpdate(() => SeekerTwitterTimer.SeekTwitter(), "*/15 * * * *");
  • 我做的代码是VB.NET语法,你的代码是C#语法
猜你喜欢
  • 1970-01-01
  • 2017-03-31
  • 2017-10-09
  • 2013-05-27
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
相关资源
最近更新 更多