【发布时间】: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