What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake an inactivity timer in Visual Basic 2005
DescriptionThis example shows how to make an inactivity timer in Visual Basic 2005.
Keywordsinactive, inactivity, user activity, time out, timeout
CategoriesWindows, Controls, ActiveX
The InactiveTimer control raises its UserInactive event after the application has been idle for a certain length of time.

The control contains an embedded Timer control that fires every second when the control is enabled. When the Tick event fires, the control calls its ElapsedIdleTime function to see how long it has been since the user interacted with the program. If that time exceeds the InactiveInterval value, the control raises its UserInactive event. (The example program closes its form when this event occurs.)

Function ElapsedIdleTime uses the GetLastInputInfo API function to learn when the user last interacted with the program. It subtracts the current time returned by Environment.TickCount and returns the result.

' See if the user has been idle for too long.
Private Sub m_Timer_Tick(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles tmrInactive.Tick
    If ElapsedIdleTime() > m_InactiveInterval Then _
        RaiseEvent UserInactive()
End Sub

' Return the number of seconds
Private Function ElapsedIdleTime() As Integer
    Dim m_lii As LASTINPUTINFO
    m_lii.cbSize = Len(m_lii)

    If GetLastInputInfo(m_lii) = 0 Then
        Throw New Exception("Error getting last input " & _
    End If
    Return Environment.TickCount - m_lii.dwTime
End Function
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.