Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to track a user's idle time in a VB.NET application

Posted on 2008-10-16
5
1,652 Views
Last Modified: 2013-12-17
Does anyone know of a way to track a users idle time in a .Net application. A sample in VB.NET or C# would be great.  We have a need to make an application either close or become partially unresponsive if a certain amount of time has passed. It needs to be application wide not just a form. We have tried the stopwatch and timer controls with marginal success. All samples we have seen so far are form specific or mention ASP.NET.  We would prefer not to use API calls to do this but if nothing else can be done then perhaps that will be the solution.

Using: Visual Studio 2005, VB.NET,  Framework 2.0, XP Pro
0
Comment
Question by:Stevejdl
5 Comments
 
LVL 22

Expert Comment

by:RedKelvin
ID: 22736906
Hi there,
You will find a complete example here
http://www.codeproject.com/KB/system/SystemIdleTimerComponent.aspx

Good luck
RedK
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22737415
Here is a completely .Net, managed code, application wide implementation that closes the app if no activity has occurred in the last 10 minutes:
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(New MyFilter)
    End Sub
 
    Private Class MyFilter
        Implements IMessageFilter
 
        Private Const WM_LBUTTONDOWN As Integer = &H201
        Private Const WM_LBUTTONUP As Integer = &H202
        Private Const WM_LBUTTONDBLCLK As Integer = &H203
        Private Const WM_RBUTTONDOWN As Integer = &H204
        Private Const WM_RBUTTONUP As Integer = &H205
        Private Const WM_RBUTTONDBLCLK As Integer = &H206
        Private Const WM_MBUTTONDOWN As Integer = &H207
        Private Const WM_MBUTTONUP As Integer = &H208
        Private Const WM_MBUTTONDBLCLK As Integer = &H209
        Private Const WM_MOUSEWHEEL As Integer = &H20A
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const WM_KEYUP As Integer = &H101
        Private Const WM_SYSKEYDOWN As Integer = &H104
        Private Const WM_SYSKEYUP As Integer = &H105
 
        Private IdleTimeInMinutes As Integer = 10
        Private WithEvents tmr As System.Timers.Timer
        Private TargetDateTime As DateTime
 
        Public Sub New()
            TargetDateTime = DateTime.Now.AddMinutes(IdleTimeInMinutes)
            tmr = New System.Timers.Timer
            tmr.Interval = 1000
            tmr.Start()
        End Sub
 
        Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
            Select Case m.Msg
                Case WM_MOUSEWHEEL, WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, WM_SYSKEYUP, _
                        WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, _
                        WM_RBUTTONDOWN, WM_RBUTTONUP, WM_RBUTTONDBLCLK, _
                        WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MBUTTONDBLCLK
 
                    TargetDateTime = DateTime.Now.AddMinutes(IdleTimeInMinutes)
            End Select
        End Function
 
        Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
            If TargetDateTime.Subtract(DateTime.Now).TotalMilliseconds < 0 Then
                tmr.Stop()
                Application.Exit()
            End If
        End Sub
 
    End Class
 
End Class

Open in new window

0
 

Author Closing Comment

by:Stevejdl
ID: 31506997
The only change made was to add a new constant:  Private Const WM_MOUSEMOVE As Integer = &H200. It was then placed at the beginning of the case statement. We wanted the app to be as responsive as the Windows screen saver.
Actually both solutions given were great but the one using managed code was preferred. Note: the other was on codeproject - http://www.codeproject.com/KB/system/SystemIdleTimerComponent.aspx
Thanks again

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22746919
WM_MOUSEMOVE is often a controversial choice for "idle activity" routines.

It is possible for the user to be interacting with OTHER applications and simply move the mouse across your application without actually interacting with it.  Should that still be considered activity for your app?  Some say yes, others no.  The user is obviously using the computer but the mouse activity may not be intended for your particular app...

Also, I found that on my system I was getting intermittent WM_MOUSEMOVE messages sent to my application by something occuring outside my application...even when the mouse wasn't being moved.  I've never figured out what causes it so I removed it from my filter since it was causing my app to never idle time out.  I'm running VB.Net 2005 Express on a WinXP Pro system.

Glad the code was helpful to you though.  =)


0
 

Expert Comment

by:BlakeMcKenna
ID: 39500786
Well...I'm using this routine and having a problem trying to display a Form. Here is the code below as well as the error.

            Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tmr.Elapsed
            If TargetDateTime.Subtract(DateTime.Now).TotalMilliseconds < 0 Then
                frmLogin.ShowDialog()
                frmMain.Enabled = False
                frmLogin.ShowDialog(frmMain)
            End If
        End Sub
screenshot.jpg
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question