Jump to content
Sign in to follow this  
FitzChivalry

_Timer_GetIdleTime() errors?

Recommended Posts

FitzChivalry

Question for everyone. I'm making an app to track computer usage in our labs here at work, and I'm using the excellent little function made by PsaltyDS called _Timer_GetIdleTime().

The problem I'm having is that every once in a while the idle time will jump from around 10 seconds to 3 hours, then back down to 10 seconds. This could be a problem either with my computer, the function, or with _TicksToTime(), but I'm not sure which or what the best way to find out for sure would be.

I'll post my code below here, along with some sample output, and if anyone could offer any help or suggestions on ways to further allow me to figure out exactly where the problem is happening, that would be great!

;time2.au3




#include <Date.au3> ; For _TicksToTime() only
#include <Process.au3>
Opt("TrayIconHide", 1)
Global $Paused
HotKeySet("{ESC}", "Terminate")

; author:  FitzChivalry at www.autoitscript.com/forum

#cs
    
    TODO
    Add activetime per application
    get windowtitle, running process, other extra information
    
    
    
    

;reg key for dell serial number is in
;HKLM\Software\Altiris\Client Service\LastUpdateComputerInfo
;Have to read and parse the reg key it looks like.....

#ce


;----------------------------------------------------------------------

$idletime = 5 ;number of minutes to wait before counting as idle time
$scantime = 30000 ;in milliseconds, so 5000 = 5 seconds
$filename = "\\altirisserver\eXpress\Apps\timescans\timelog2.csv"
$var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Altiris\Client Service", "LastUpdateComputerInfo")
$Serial = StringRegExp($var, "(?:Serial)(?:-Number=)([[:alnum:]]{7})", 1)
$activeperhour = 0
$startactive = 0
$write = 0

;-----------------------------------------------------------------------

;ConsoleWrite("starting script")
While 1

    $idle = _Timer_GetIdleTime()
    ;MsgBox(4096,"",$idle)



    Global $Hr, $Min, $Sec
    _TicksToTime($idle, $Hr, $Min, $Sec)
    ConsoleWrite("Debug: Converted = " & $Hr & ":" & $Min & ":" & $Sec & @LF)

    scan()

    Sleep($scantime)

WEnd


Func scan()
    ;so if idle is greater than the idletime(10 min), then i need to increase idletimeperhour.  That by itself would be pretty easy to do, but I also need to make sure that each time the hour switches
    ;it records the total idle time for that hour.
    If @MIN = 02 Then
        $write = 0
        ConsoleWrite("write is now 0")
    EndIf
    
    
    ;how to make it so that it writes the time in the correct hour?
    If @MIN == 59 And $write = 0 Then
        ConsoleWrite("writing to file!")
        $line = $Serial[0] & ", " & @ComputerName & ", " & @UserName & ", " & _NowDate() & ", " & @HOUR & ", " & $activeperhour
        $file = FileOpen($filename, 1)
        FileWriteLine($file, $line)
        FileClose($file)
        
        $activeperhour = 0
        $write = 1
        
    Else
        If $Min < $idletime And $Hr = 0 Then
            
            $activeperhour = $activeperhour + (60000/$scantime);This gets the fraction of the minute to increment activetime
            ConsoleWrite("Incremented activetime, it is: " & $activeperhour & @LF)
        EndIf

    EndIf


EndFunc   ;==>scan


Func Terminate()
    Exit 0
EndFunc   ;==>Terminate


; ================================================
; function:  _Timer_GetIdleTime()
; purpose:  Returns the number of ticks since last user activity (i.e. KYBD/Mouse)
; syntax:  _Timer_GetIdleTime()
; returns:  On success:  integer ticks since last (approx. milliseconds) since last activity
; notes:  The current ticks since last system restart will roll over to 0 every 50 days or so,
;      which makes it possible for last user activity to be before the rollover, but run time
;      of this function to be after the rollover.  If this happens, @extended = 1 and the
;      returned value is ticks since rollover occured.
; author:  PsaltyDS at www.autoitscript.com/forum
; ================================================
; Change log:
; v1.0.0  --  03/20/2008 First version
; ================================================
Func _Timer_GetIdleTime()
    ; Get ticks at last activity
    Local $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))

    ; Get current ticks since last restart
    Local $aTicks = DllCall("Kernel32.dll", "int", "GetTickCount")

    ; Return time since last activity, in ticks (approx milliseconds)
    Local $iDiff = $aTicks[0] - DllStructGetData($struct, 2)
    If $iDiff > 0 Then
        ; Normal return
        Return $iDiff
    Else
        ; Rollover of ticks counter has occured
        Return SetError(0, 1, $aTicks[0])
    EndIf
EndFunc   ;==>_Timer_GetIdleTimeoÝ÷ Ù&¦¦W¨ºÚn¶ØZ¶ÈhÂËajÛ(ëaxºy2È(x(«­¢+ÙÕè
½¹ÙÉÑôÀèÀèÌ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÈà)Õè
½¹ÙÉÑôÈèäèÈÀ)Õè
½¹ÙÉÑôÀèÀèÜ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÈà¸Ô)Õè
½¹ÙÉÑôÀèÀèÈ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÈä)Õè
½¹ÙÉÑôÀèÀèÌÈ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÈä¸Ô)Õè
½¹ÙÉÑôÀèÀèÀ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÌÀ)Õè
½¹ÙÉÑôÀèÀèÄ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÌÀ¸Ô)Õè
½¹ÙÉÑôÀèÀèÀ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÌÄ)Õè
½¹ÙÉÑôÀèÀèÄÈ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÌĸÔ)Õè
½¹ÙÉÑôÈèÄÌèÈÀ)Õè
½¹ÙÉÑôÀèÀèÀ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÌÈ)Õè
½¹ÙÉÑôÀèÀèÀ)%¹Éµ¹ÑÑ¥ÙÑ¥µ°¥Ð¥ÌèÌȸÔ)Õè
½¹ÙÉÑôÀèÀèÄ

Share this post


Link to post
Share on other sites
PsaltyDS

To see if you are getting strange values back from _Timer_GetIdleTime(), just change your debug display to this:

ConsoleWrite("Debug: Unconverted = " & $idle & "  Converted = " & $Hr & ":" & $Min & ":" & $Sec & @LF)

Please post the results.

:)

Simplified test script:

#include <Date.au3>; For _TicksToTime() only

HotKeySet("{ESC}", "Terminate")

Global $iTimer = TimerInit(), $Hr, $Min, $Sec

While 1
; Every 10 sec
    If TimerDiff($iTimer) >= 10000 Then
        $idle = _Timer_GetIdleTime()
        _TicksToTime($idle, $Hr, $Min, $Sec)
        ConsoleWrite("Debug: " & @HOUR & ":" & @MIN & ":" & @SEC & "  Unconverted = " & $idle & "  Converted = " & $Hr & ":" & $Min & ":" & $Sec & @LF)
        $iTimer = TimerInit()
    EndIf
WEnd

Func Terminate()
    Exit 0
EndFunc  ;==>Terminate

; ================================================
; function:  _Timer_GetIdleTime()
; purpose:  Returns the number of ticks since last user activity (i.e. KYBD/Mouse)
; syntax:  _Timer_GetIdleTime()
; returns:  On success:  integer ticks since last (approx. milliseconds) since last activity
; notes:  The current ticks since last system restart will roll over to 0 every 50 days or so,
;     which makes it possible for last user activity to be before the rollover, but run time
;     of this function to be after the rollover.  If this happens, @extended = 1 and the
;     returned value is ticks since rollover occured.
; author:  PsaltyDS at www.autoitscript.com/forum
; ================================================
; Change log:
; v1.0.0  --  03/20/2008 First version
; ================================================
Func _Timer_GetIdleTime()
; Get ticks at last activity
    Local $struct = DllStructCreate("uint;dword");
    DllStructSetData($struct, 1, DllStructGetSize($struct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($struct))

; Get current ticks since last restart
    Local $aTicks = DllCall("Kernel32.dll", "int", "GetTickCount")

; Return time since last activity, in ticks (approx milliseconds)
    Local $iDiff = $aTicks[0] - DllStructGetData($struct, 2)
    If $iDiff > 0 Then
    ; Normal return
        Return $iDiff
    Else
    ; Rollover of ticks counter has occured
        Return SetError(0, 1, $aTicks[0])
    EndIf
EndFunc  ;==>_Timer_GetIdleTime
Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
FitzChivalry

lol, why didn't I think of that?????

Here's the output:

Debug: Unconverted = 687  Converted = 0:0:1
Incremented activetime, it is: 0.5
Debug: Unconverted = 11011765  Converted = 3:3:32
Debug: Unconverted = 8906  Converted = 0:0:9
Incremented activetime, it is: 1
Debug: Unconverted = 38906  Converted = 0:0:39
Incremented activetime, it is: 1.5
Debug: Unconverted = 68906  Converted = 0:1:9
Incremented activetime, it is: 2
Debug: Unconverted = 98921  Converted = 0:1:39
Incremented activetime, it is: 2.5
Debug: Unconverted = 128921  Converted = 0:2:9
Incremented activetime, it is: 3
Debug: Unconverted = 4718  Converted = 0:0:5
Incremented activetime, it is: 3.5
Debug: Unconverted = 11221796  Converted = 3:7:2
Debug: Unconverted = 11251812  Converted = 3:7:32
Debug: Unconverted = 28953  Converted = 0:0:29
Incremented activetime, it is: 4
Debug: Unconverted = 11311812  Converted = 3:8:32

It seems like the strange values are steadily incrementing along, it goes from 3:7:32 to 3:8:32 1 minute later. So it looks like it is tracking something, but I'm not sure what...

[EDIT] Didnt' see your simplified script, this output was done by just modifying my own script... I'll run yours to see if it changes the output, there could be something funky in my script as well [\EDIT]

Edited by FitzChivalry

Share this post


Link to post
Share on other sites
FitzChivalry

Here's the output for your simplified program:

Debug: 16:00:29  Unconverted = 10500  Converted = 0:0:11
Debug: 16:00:39  Unconverted = 20500  Converted = 0:0:21
Debug: 16:00:49  Unconverted = 30500  Converted = 0:0:31
Debug: 16:00:59  Unconverted = 11801718  Converted = 3:16:42
Debug: 16:01:09  Unconverted = 9437  Converted = 0:0:9
Debug: 16:01:19  Unconverted = 19437  Converted = 0:0:19
Debug: 16:01:29  Unconverted = 2359  Converted = 0:0:2
Debug: 16:01:39  Unconverted = 172  Converted = 0:0:0
Debug: 16:01:49  Unconverted = 11851718  Converted = 3:17:32

It seems that maybe this only happens if the computer is active at exactly the time it checks for the idle time? I was just bouncing my mouse around the screen, and every time I kept the mouse moving rapidly it gave back the strange numbers. That could be just a coincidence though.

Edited by FitzChivalry

Share this post


Link to post
Share on other sites
PsaltyDS

Here's the output for your simplified program:

It seems that maybe this only happens if the computer is active at exactly the time it checks for the idle time? I was just bouncing my mouse around the screen, and every time I kept the mouse moving rapidly it gave back the strange numbers. That could be just a coincidence though.

Ah Ha! It's happening when the detected idle time is 0.

Change the function detection of rollover from '$iDiff > 0' to '$iDiff >= 0':

If $iDiff >= 0 Then
   ; Normal return
        Return $iDiff
    Else
   ; Rollover of ticks counter has occured
        Return SetError(0, 1, $aTicks[0])
    EndIf

Thanks for catching the bug!

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
FitzChivalry

Sweet! Thanks for your help, I'll be making good use of this for my program, I'll post it over in example scripts once it's polished up a bit. Thanks again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×