使用定时器导致HttpContext.Current为空 [英] use of timer causes HttpContext.Current to be null
问题描述
我有解析输入文件的功能。
I have a function that parses an input file.
Private Function getSvSpelOdds(ByVal BombNo As Integer) As Boolean
Dim InputFileBase As String = HttpContext.Current.Application("InputFileBase")
strInputFile = InputFileBase & "PC_P7_D.TXT"
OddsReader = New StreamReader(strInputFile)
'some other code
End Function
如果该文件不存在(getSvSpelOdds返回False),我想在30秒后重试。
要做到这一点我用一个定时器。
If the file is not there (getSvSpelOdds returns False), I would like to retry after 30 seconds. To achieve this I use a timer.
If Not getSvSpelOdds(y) Then
Timer1.Interval = 30000
End If
Private Sub Timer1_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
getSvSpelOdds(y)
End Sub
问题是,当定时器触发HttpContext.Current(用于获取gloal变量的值)为null。
Problem is that when timer fires the HttpContext.Current (used to get the value of gloal variable) is null.
我应该用一些其他的方法来得到这个工作?
Should I use some other approach to get this to work?
推荐答案
如前所述的HttpContext应为空。但是你可以使用System.Web.HttpRuntime.Cache来传递文件名,缓存应该从所有线程访问。
As already described HttpContext should be null as Timer_Elapsed is called in different thread. But you may use System.Web.HttpRuntime.Cache to pass filename, cache should be accessible from all threads.
这篇关于使用定时器导致HttpContext.Current为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!