2016-10-07 72 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 
相關問題