1

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       

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

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?

user1859309
  • 69
  • 3
  • 12
  • possible duplicate of [How does HttpContext.Current work in a multi-threaded environment?](http://stackoverflow.com/questions/1561036/how-does-httpcontext-current-work-in-a-multi-threaded-environment) – Lex Li Sep 06 '13 at 07:29

4 Answers4

1

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.

Max Markov
  • 924
  • 12
  • 23
0

HttpContext.Current only gives you the context you want when you call it on the thread that handles the incoming thread.

When calling it outside of such threads, you get null. That matches your case, as Timer1_Elapsed is executed on a new thread.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
0

The Elapsed event on the Timer will run on a separate thread therefore its expected behaviour for the current context to be null.

You can only access it from the same thread.

Should I use some other approach to get this to work?

Yes, it's not generally a good idea to mix ASP.NET and threads given the complexity of how ASP.NET works. Like already mentioned its not a great UX to have no feedback for 30 seconds, its better to let the user know what's actually going on.

Also, you need to determine whether the timeout length is appropriate or whether a timeout is needed at all. I don't know the nature of your application but I assume there is some external means for the file to be generated and picked up by your site.

James
  • 80,725
  • 18
  • 167
  • 237
0

Should I use some other approach to get this to work?

Almost certainly, yes. 30 seconds is a long time to wait without giving any feedback to users.

It would probably be better to return a "no results are available yet, but we're still looking" page to the user. That page can be set to refresh automatically after 30 seconds, by adding a suitable meta-tag:

<META HTTP-EQUIV="refresh" CONTENT="30">

And you then get a fresh request/response cycle on the server. And haven't tied up server resources in the meantime.


Other answers seems to address the other part of your question (about why it doesn't work in the timer callback)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448