[Solved] User idle detection problem
Posted: Sun Feb 25, 2018 10:21 pm
[Edit] Solved - See my post further below. 
I need my app to know when the user hasn't used the PC (ie. typed or used the mouse) for 60 seconds. However, the small test code below gives wrong results: waiting for 5000 ms actually results in 5800 or 6100 ms, and waiting for 60000 ms (one minute) results in 70000 ms instead (a massive 10 seconds too long). The plan is that my app will call GetIdleTime() once per minute in a thread, and if unused, the thread will write something to a log.
Important note: The code below on PureBasic v5.20 (32-bit) works as expected and the timing is correct. But I'm using PureBasic v5.62 (32-bit). I'm guessing the issue is something to do with GetTickCount_()? My PC hasn't been running for 49.7 days yet.

I need my app to know when the user hasn't used the PC (ie. typed or used the mouse) for 60 seconds. However, the small test code below gives wrong results: waiting for 5000 ms actually results in 5800 or 6100 ms, and waiting for 60000 ms (one minute) results in 70000 ms instead (a massive 10 seconds too long). The plan is that my app will call GetIdleTime() once per minute in a thread, and if unused, the thread will write something to a log.
Important note: The code below on PureBasic v5.20 (32-bit) works as expected and the timing is correct. But I'm using PureBasic v5.62 (32-bit). I'm guessing the issue is something to do with GetTickCount_()? My PC hasn't been running for 49.7 days yet.

Code: Select all
Procedure GetIdleTime()
lipi.LASTINPUTINFO\cbSize=SizeOf(LASTINPUTINFO)
GetLastInputInfo_(@lipi)
ProcedureReturn (GetTickCount_()-lipi\dwTime)
EndProcedure
Debug "Press Ctrl to start timing..."
Repeat
Sleep_(1)
Until GetAsyncKeyState_(#VK_CONTROL)<>0
Debug "Timing (don't use your PC)..."
start.q=ElapsedMilliseconds()
Repeat
Sleep_(1)
Until GetIdleTime()>5000
Debug Str(ElapsedMilliseconds()-start)+" ms actually passed :("