我正在尝试为我的 vb.net 桌面应用程序实现自动注销功能。这是代码:
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
问题是当我将注销时间设置为 1 分钟时,如果我不在我的应用程序上做任何事情,它会在 1 分钟后给我注销窗口。但是在我取消我的注销窗口消息并开始在我的应用程序上工作后,注销消息框不断出现。首先我认为我的计时器没有正确重置,但如果没有 myTimer.Enabled=False 应该可以解决问题。 Start() 绝对应该做到这一点,但这似乎也不起作用。我检查了我的预过滤器消息,但仍然没有运气。
任何帮助将非常感激。
谢谢。