Detecting Systemwide User Idle Time in Windows XP with Visual Basic .NET

Posted on 2003-03-16
Medium Priority
Last Modified: 2013-11-25
Hello all,
          This might be a silly question but I just can't figure it out and any help would be greatly appreciated.

I am trying to code a routine in Visual Basic .NET that will tell me how long the user has been idle (how long since no keyboard and mouse input has been detected.) If idle time exceeds a certain ammount of minutes, my program needs to pop up a message and do other things.

I have tried to use the API GetLastInputInfo in order retrieve this information, but it is just not working.  Here is the code I am using:

In a module, I have added:

    Public Structure LastInputInfo
        Public cbSize As Long
        Public dwTime As Long
    End Structure

    Declare Function GetLastInputInfo Lib "User32" (ByRef lastinfo As LastInputInfo) As Boolean

And my form code reads something like this (I have ommited Windows Form Designer Generated Code for the sake of clarity):

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

        'Timer is set to run every second

        Dim res As Boolean
        Dim info As LastInputInfo

        info.cbSize = Len(info)
        res = GetLastInputInfo(info)
        MessageBox.Show(res & " " & info.dwTime.ToString)
          ' This messagebox is there for debug purposes.  It ends up showing that res is False and
          '  info.dwTime.ToString is 0; indicating the function has failed.

          ' I have also omitted the code to test for elapsed idle time, etc, since the point here is to
          '   show the function is failing.

End Sub

What I don't understand is why the function is failing.  I must be doing something wrong, either when declaring it, or when calling it.

I don't necessarily have to use GetLastInputInfo, I just thought using that API would be the easiest and 'cleanest' way of telling how long the system has been idle.  I am open for other solutions as well.  Just keep in mind my program is coded in Visual Basic .NET and I know nothing of C++.

Any help greatly appreciated. Thank you very much!
Question by:Spanda
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
LVL 10

Accepted Solution

Jason Evans earned 200 total points
ID: 8147499

Hi there.

Try using the following code in your form:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.ANSI)> Public Structure LastInputInfo
    Public cbSize As Int32
    Public dwTime as int32
End Structure

Private Declare Function GetLastInputInfo Lib "User32.dll" (<MarshalAs(UnmanagedType.Struct)> ByRef lpLastInputInfo As LastInputInfo) As Int32  

I tried this just now and it works fine. The new method of passing types/structures in VB.NET can be a bit scary at first, but u get used to it.

Hope this helps.

Expert Comment

ID: 8147966
Another way to do this is to set up a system wide hook on all keyboard and mouse inputs, this everytime you get one, just "reset" your start counter and disable, reenable a timer.  If the timer fires (time has elasped) then the system has been idle for your elasped time. This is a method to keep you from "polling" the system and chewing up a lot of cycles.

if you need more detail, let me know.


Author Comment

ID: 8155236
Thank you for the quick response.  This has fixed my problem.  If I may add, one needs to add the following line of code in order for the declarations to work: Imports System.Runtime.InteropServices

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

752 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