Javier Garzon Javier Garzon - 2 months ago 8
Vb.net Question

Is it possible remove a session variable with a timer?

I want to remove a session variable using a timer. I have this class.

Public Class SessionKiller
Private WithEvents mclsTimer As Timer
Private mstrSessionKey As String

Public Sub New(ByVal lstrSessionKey As String)
mstrSessionKey = lstrSessionKey

mclsTimer = New Timer(3000)
mclsTimer.AutoReset = False
mclsTimer.Start()
End Sub

Private Sub OnTimedEvent(ByVal lobjSource As Object, lclsEvent As ElapsedEventArgs) Handles mclsTimer.Elapsed
HttpContext.Current.Session.Remove(mstrSessionKey)
End Sub
End Class


But the
current
variable is nothing. Is it possible remove a session variable this way?

Answer

I solved using a pointer to session var.

Public Class SessionKiller
    Private WithEvents mclsTimer As Timer
    Private mstrSessionKey As String
    Private mclsSession As HttpSessionStateBase

    Public Sub New(ByVal lstrSessionKey As String, _
                   ByVal lclsSession As HttpSessionStateBase)
        mstrSessionKey = lstrSessionKey
        mclsSession = lclsSession

        mclsTimer = New Timer(3000)
        mclsTimer.AutoReset = False
        mclsTimer.Start()
    End Sub

    Private Sub OnTimedEvent(ByVal lobjSource As Object, lclsEvent As ElapsedEventArgs) Handles mclsTimer.Elapsed
        mclsSession.Remove(mstrSessionKey)
    End Sub
End Class