I should think a network policy to lock the desktop after a perips of inactivity would be the simplest: no need to save any data (as per a previous question).
Since you are emulating the Windows job of tracking the idle time, you could use the API to get the idle time:
Partial Friend Class NativeMethods
<StructLayout(LayoutKind.Sequential)>
Structure LASTINPUTINFO
<MarshalAs(UnmanagedType.U4)>
Public cbSize As Integer
<MarshalAs(UnmanagedType.U4)>
Public dwTime As Integer
End Structure
' Gets the time of the last user input in ms
<DllImport("user32.dll")>
Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
End Function
Friend Shared Function GetLastInputTimeElapsed() As Int32
Dim lastInputInf As New LASTINPUTINFO()
lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
lastInputInf.dwTime = 0
' get time of last input, subtract current ticks
If GetLastInputInfo(lastInputInf) Then
' conver to seconds
Return (Environment.TickCount - lastInputInf.dwTime) \ 1000
Else
Return 0
End If
End Function
...
Then just check the elapsed seconds (or minutes) in a long timer:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
' Gets the time of the last user input in ms
Dim elapsed = NativeMethods.GetLastInputTimeElapsed()
' 45 sec TimeOut for testing
If elapsed > 45 Then
Label3.Text = "You're done!"
Else
Label3.Text = String.Format("Idle for: {0} secs", elapsed)
End If
End Sub
Animated GIFs dont run fast enough to show, but input is detected in the form of any mousemove or keypress. Note that this detects system input - mouse moves and key pressed with your app minimized will count. This makes some sense - if the app is minimized and times itself out, I'd be suprised to see it just disappear. If I am totally AFK, thats another thing.
Local Timer
For a simple form, the answer for VB Detect Idle time is pretty simple: it stops and restarts your 30 min timer whenever it sees a mousemove or keypress. It doesnt detect movement on container controls though.
Using a Stopwatch will work too:
' form/class level
Private swLogout As Stopwatch
' start it with the app:
swLogout = New Stopwatch()
swLogout.Start()
You need to restart it for each keypress and mousemove. For the first, set KeyPreview
to true, then:
Private Sub MainFrm_KeyPress(sender As Object,
e As KeyPressEventArgs) Handles MyBase.KeyPress
swLogout.Restart()
End Sub
Mousemove is more problematic, you need to hook up all the major container controls:
Private Sub MainFrm_MouseMove(sender As Object,
e As MouseEventArgs) Handles MyBase.MouseMove,
TabControl1.MouseMove, TabControl2.MouseMove,
..., TabPage12.MouseMove
swLogout.Restart()
End Sub
Then check the elapsed time in a fairly short timer (like 3 mins maybe):
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim elapsed = swLogout.ElapsedMilliseconds \ 1000
' demo == 3 secs
If elapsed > 3 Then
Label3.Text = "You're done!"
Timer2.Enabled = False
Else
Label3.Text = String.Format("Idle for: {0} secs", elapsed)
End If
End Sub