Link to home
Start Free TrialLog in
Avatar of Lee W, MVP
Lee W, MVPFlag for United States of America

asked on

VB6 Get Idle Time of system

I'm trying to determine the idle time of the system.  I was trying to use this code:

Private Type LastInputInfoType
    Size As Long    'Bytes
    Time As Long    'Dword
End Type

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function GetLastInputInfo Lib "user32" (plii As Any) As Long

Public Function IdleTime() As Long
    Dim LastInputInfo As LastInputInfoType
    
    LastInputInfo.Size = Len(LastInputInfo)
    GetLastInputInfo LastInputInfo
    
    IdleTime = GetTickCount - LastInputInfo.Time / 1000
    MainForm.Debug1Text = GetTickCount
    MainForm.Debug2Text = IdleTime
    MainForm.DebugText = "Time: " & LastInputInfo.Time & vbCrLf & "Size: " & LastInputInfo.Size
    
End Function

Open in new window


But it's not working.  When I put the variables in a text box to see what's going on, I'm getting numbers in the billions for both with the LastInputInfo.Time about 15-16 (varying) less than the GetTickCount value.  If I'm reading this, LastInputInfo.Time should be static UNLESS I'm using things... but it's always sensing activity and I'm not sure why...

I'm using a timer control to check every 100 ms - IdleTime is requested every timer cycle.

I'm open to other methods if anyone has them to detect idle time...
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lee W, MVP

ASKER

For my purposes, the resolution doesn't need to be high.  The timer is set to 100 and that's just so I can update a second counter.  When I did what you describe I'm able to see the counter reset and tick away during idle so it's doing exactly what I need it to do.  I didn't know about the timer wrap at 49.7 days... but in most cases, thanks to Microsoft forcing you to reboot, that shouldn't be a problem on my target systems.

Thanks for the assist!
-Lee
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2018
              Experts Exchange Top Expert VBA 2018