I am trying to implement auto log off feature for my vb.net desktop application. Here is the code:
Private Sub AutoLogOffTimer(ByVal myUIContext As globals)
If myUIContext.parameters.LogOutTime <> 0 Then
myTimer = New System.Windows.Forms.Timer()
myTimer.Enabled = False
'myTimer.Dispose()
myTimer.Interval = (myUIContext.parameters.LogOutTime * 60 * 1000) - 20000
AddHandler myTimer.Tick, AddressOf logOutUser
myTimer.Start()
Application.AddMessageFilter(Me)
ElseIf myUIContext.parameters.LogOutTime = 0 Then
Application.RemoveMessageFilter(Me)
RemoveHandler myTimer.Tick, AddressOf logOutUser
myTimer.Stop()
myTimer.Enabled = False
End If
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
' Monitor message for keyboard and mouse messages
Dim active As Boolean = m.Msg = &H100 OrElse m.Msg = &H101
' WM_KEYDOWN/U
'active = active OrElse m.Msg = &HA0 ' OrElse m.Msg = &H200
active = active OrElse m.Msg = &HA3 OrElse m.Msg = &H201 OrElse m.Msg = &H202 OrElse m.Msg = &H1 OrElse m.Msg = &H200 OrElse m.Msg = &H2 OrElse m.Msg = &H208 OrElse m.Msg = &H2A3 OrElse m.Msg = &H2A1
' WM_(NC)MOUSEMOVE
active = active OrElse m.Msg = &H10 OrElse m.Msg = &H3 OrElse m.Msg = &H5
' WM_CLOSE, in case dialog closes
If active Then
myTimer.Enabled = False
myTimer = New System.Windows.Forms.Timer()
If _myUIContext.parameters.LogOutTime <> 0 Then
myTimer.Interval = (_myUIContext.parameters.LogOutTime * 60 * 1000) - 20000
Try
RemoveHandler myTimer.Tick, AddressOf logOutUser
Catch ex As Exception
End Try
AddHandler myTimer.Tick, AddressOf logOutUser
myTimer.Start()
myTimer.Enabled = True
ElseIf _myUIContext.parameters.LogOutTime = 0 Then
myTimer = New System.Windows.Forms.Timer()
myTimer.Stop()
End If
End If
Return False
End Function
Private Sub logOutUser(ByVal sender As Object, ByVal e As EventArgs)
If Not isLogoutWindowOpen Then
If ComputeCpuUsage() < 1 Then
LogoutWindow = New LogoutApp(20)
AddHandler LogoutWindow.logOutCancel, AddressOf logOutCancel
AddHandler LogoutWindow.logOutProceed, AddressOf logOutProceed
LogoutWindow.MdiParent = Me
LogoutWindow.Show()
myTimer.Stop()
isLogoutWindowOpen = True
Else
myTimer.Enabled = False
myTimer.Start()
End If
End If
End Sub
Private Sub logOutCancel()
myTimer = Nothing
myTimer = New System.Windows.Forms.Timer()
myTimer.Enabled = False
myTimer.Stop()
If _myUIContext.parameters.LogOutTime <> 0 Then
myTimer.Interval = (_myUIContext.parameters.LogOutTime * 60 * 1000) - 20000
myTimer.Start()
End If
myTimer.Enabled = True
isLogoutWindowOpen = False
End Sub
The problem is when i set the log out time to be lets say 1 minute, it works fine if and gives me the logout window after 1 minute if i do not do anything on my application. But after I cancel my logout window message and start working on my application the log out message box keeps coming up arbitrarily .First I thought my timers were not getting reset properly but myTimer.Enabled=False should have done the trick if no that myTimer.Start() should have definitely done the trick, but that that does not seem to work also. i have checked my pre-filter messages but still no luck.
Any help would be much appreciated.
Thanks.
OK I think I might have figured out your problem.
Sometimes, Windows timers don’t care if they have been null referenced, and continue to fire their events without a parent. I think your problem lies in that in your logOutCancel code you are clearing the reference to the timer, but not first disabling it.
Try a call to myTimer.Stop() before your line where you set myTimer = Nothing.
Alternatively, why do you need to keep creating new Timers at all? Can’t you just stop then restart your original one?