2016-10-07 34 views
0

我想使用计时器删除会话变量。我有这个班。是否有可能用定时器删除会话变量?

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 

但是current变量不算什么。这样可以删除会话变量吗?

+0

请在您获取/创建会话的位置添加代码 – theBugger

回答

0

我解决了使用指针session变种。

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 
相关问题