我目前正在研究一个将在一定数量的非活动时间内注销用户的方法。我宣布 Application.Idle
Private Sub Application_Idle(sender As Object, e As EventArgs)
Timer.Interval = My.Settings.LockOutTime
Timer.Start()
End Sub
然后,在表单加载事件中调用它
Private Sub ctlManagePw_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddHandler System.Windows.Forms.Application.Idle, AddressOf Application_Idle
End Sub
在计时器上
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
Try
If My.Settings.TrayIcon = 1 Then
Me.ParentForm.Controls.Remove(Me)
control_acPasswords()
_Main.NotifyIcon.Visible = True
_Main.NotifyIcon.ShowBalloonTip(1, "WinVault", "You've been locked out due to innactivity", ToolTipIcon.Info)
End If
'Stop
Timer.Stop()
Timer.Enabled = False
'Flush memory
FlushMemory()
Catch ex As Exception
'Error is trapped. LOL
Dim err = ex.Message
End Try
End Sub
这样做的问题是,每当空闲事件结束时,我仍然会收到通知说我再次被锁定和/或应用程序已进入空闲事件。
control_acPasswords()
是注销用户控件
这是我释放内存的地方
Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal process As IntPtr, ByVal minimumWorkingSetSize As Integer, ByVal maximumWorkingSetSize As Integer) As Integer
Public Sub FlushMemory()
Try
GC.Collect()
GC.WaitForPendingFinalizers()
If (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1)
Dim myProcesses As Process() = Process.GetProcessesByName(Application.ProductName)
Dim myProcess As Process
For Each myProcess In myProcesses
SetProcessWorkingSetSize(myProcess.Handle, -1, -1)
Next myProcess
End If
Catch ex As Exception
Dim err = ex.Message
End Try
End Sub
如果我放置一个MsgBox(ex.Message)
Timer_Tick 事件异常,我会不断收到
Object reference not set to an instance of an object
我的预期结果是,每当表单进入 Idle 事件时,它将获取间隔或时间,My.Settings.LockOutTime
其中是分钟值并存储为60000
for 1 minute
or60 seconds
并启动计时器。现在在 Timer_Tick 上logout
,如果间隔结束,则用户。
我处理事件的方式有什么问题吗?