Jump to content
Sign in to follow this  
Pixelz

_Timer_GetIdleTime() returns negative number

Recommended Posts

Pixelz

Using the following script, I get a negative value returned:

#include <Timers.au3>

$idle = _Timer_GetIdleTime()
ConsoleWrite($idle & @CRLF)

My understanding is that this is supposed to return a positive value?

This is on xp.

I've done some searching for this but I haven't found anyone with the same problem

Edit: I tried it on a different computer, and it seems to work fine there. The box I tried it on initially has a rather long uptime, perhaps that's related?

Edited by Pixelz

Share this post


Link to post
Share on other sites
trancexx

PsaltyDS is using signed integer as a return type of GetTickCount function thus causing wrong return value of that function under some conditions (if > ~24,855 days passed)


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Pixelz

PsaltyDS is using signed integer as a return type of GetTickCount function thus causing wrong return value of that function under some conditions (if > ~24,855 days passed)

How do I change it to something that works for higher uptimes?

Edit: I change it to dword which seems to at least return a positive value, I'm not sure if it breaks anything else though..

Edited by Pixelz

Share this post


Link to post
Share on other sites
trancexx

How do I change it to something that works for higher uptimes?

Edit: I change it to dword which seems to at least return a positive value, I'm not sure if it breaks anything else though..

This is the function:

; #FUNCTION#;===============================================================================
; Name...........: _Timer_GetIdleTime
; Description ...: Returns the number of ticks since last user activity (i.e. KYBD/Mouse)
; Syntax.........: _Timer_GetIdleTime()
; Parameters ....: None
; Return values .: Success - integer ticks since last (approx. milliseconds) since last activity
;                 Failure - Sets @extended = 1 if rollover occurs (see remarks)
; Author ........: PsaltyDS at http://www.autoitscript.com/forum
; Modified.......:
; Remarks .......: 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.
; Related .......:
; Link ..........;
; Example .......; Yes
;;==========================================================================================
Func _Timer_GetIdleTime()
; Get ticks at last activity
    Local $tStruct = DllStructCreate("uint;dword");
    DllStructSetData($tStruct, 1, DllStructGetSize($tStruct));
    DllCall("user32.dll", "int", "GetLastInputInfo", "ptr", DllStructGetPtr($tStruct))

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

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

Change

Local $avTicks = DllCall("Kernel32.dll", "int", "GetTickCount")
to
Local $avTicks = DllCall("Kernel32.dll", "dword", "GetTickCount")
and try.

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Pixelz

Change

Local $avTicks = DllCall("Kernel32.dll", "int", "GetTickCount")
to
Local $avTicks = DllCall("Kernel32.dll", "dword", "GetTickCount")
and try.
Done & done. Seems to work. This might be something that shoud be considered changing in the UDF though since I can imagine there's more people than me who have an uptime higher than 25 days...

Share this post


Link to post
Share on other sites
KaFu

Look at help-file, GetLastInputInfo will return wrong result after > 49 days anyhow (even with the correction above). Do an additional check on @extended if you expect the uptime might be higher then that.

Share this post


Link to post
Share on other sites
trancexx

PsaltyDS is known to be very pedantic. I'm sure it will be fixed.

Btw, I'm more concern about him not checking errors after the DllCall() there. He checks errors after almost anything (he passed this behaviour to me actually).


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
PsaltyDS

PsaltyDS is known to be very pedantic. I'm sure it will be fixed.

I'm sure you meant that in a constructive, supportive, affirming way... ^_^

Using dword return type would be better (and would more acurately reflect the type shown in MSDN). I didn't make a call one way or the other, however, as that call was lifted straight out of Date.au3 in the _Date_Time_GetTickCount() function. Probably should be tweaked in both places.

Btw, I'm more concern about him not checking errors after the DllCall() there. He checks errors after almost anything (he passed this behaviour to me actually).

I looked through the UDFs and didn't find the Devs checking for errors from DLLs that should never fail. If Kernel32.dll isn't available or the system timer can't be accessed then you don't even have a working instance of Windows running, let alone your AutoIt script. The MSDN description of GetTickCount doesn't show any status returned at all. So I copied that behavior where low-level functions that MUST work (if Windows does) are involved.

I'm not a "real" programmer myself, just pick that up from the UDFs and this forum. So it could be wrong, but leaves me in good company.

;)

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
trancexx

I'm sure you meant that in a constructive, supportive, affirming way... ^_^

I tend to be misinterpreted lately. To be on the safe side here... yes.

Other part of your post is based on argumentum ad verecundiam and that makes it not worth commenting. Appeal to authority is just another logical fallacy.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
PsaltyDS

I tend to be misinterpreted lately. To be on the safe side here... yes.My reply was only meant to be humorous, I didn't think your comment was meant badly and the reply wasn't meant to make you feel defensive.

:(

Other part of your post is based on argumentum ad verecundiam and that makes it not worth commenting. Appeal to authority is just another logical fallacy.

I wasn't aware we were engaged in argument, let alone a formalized one. That seems a strange place to go on an all-volunteer forum for a freeware scripting language. To speak with the same jargon: Informal discourse would be more appropriate to the context and venue than formal dialectics.

I've been away from the forum for a little while. Are you having some issues?

;)


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

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  

×