Idle time

Hi,

I am trying to have my program time out if the user lets it sit idle for an amount of time. Is there an easy way to find the last user input? I am useing VB.NET.

Thenks Mike

[182 byte] By [MRMOU812] at [2007-12-16]
# 1
Yes, and no.
Basically, you can add a handler for Application.Idle, and simply check the time from a loop statement.
Here's a sample. WarnIdle is used to turn the warning on / off.
On the form Load, i add a OnIdle handler. On Form Close, i remove that handler.
During the idle loop, i allow a 100ms sleep, as to not take all the cpu power :)



Public Class Form1
Private WarnIdle As Boolean = False
Private LastTime As Date
<System.Security.SuppressUnmanagedCodeSecurity(), System.Runtime.InteropServices.DllImport("kernel32.dll")> _
Public Shared Sub Sleep(ByVal dwMilliseconds As Long)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WarnIdle = True
LastTime = Date.Now()
AddHandler Application.Idle, AddressOf OnIdle
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
RemoveHandler Application.Idle, AddressOf OnIdle
End Sub
Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
LastTime = Date.Now
End Sub
Public Sub OnIdle(ByVal sender As Object, ByVal e As EventArgs)
Do While Me.Created
Dim thetime As TimeSpan = Date.Now() - LastTime
If thetime.Seconds > 5 And WarnIdle Then
MsgBox("You are Idle")
LastTime = Date.Now
End If
Application.DoEvents()
Sleep(100)
Loop
End Sub
End Class


Hope this helps,
Dustin.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
The way I'm doing it is starting/stoping a timer in the WndProc of the main form. If the timer ever expires it's been idle too long.
TaDa at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Thanks that helped alot. I have it working now.
Thanks Again Mike
MRMOU812 at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...