Jump to content
Sign in to follow this  
lgnihlman

Performance issue

Recommended Posts

lgnihlman

I found a script (see below) in the forums that checks for keyboard and mouse inactivity.

However when I compile and run the resulting .exe file it takes 10% CPU time (Win XP Pro SP2).

Is there an other way to monitor keyboard and mouse inactivity that won't take that much CPU time ?

Could the script be modified to be less CPU intensive ?

Maybe it's possible to use Windows own way to monitor inactivity ?

Any ideas are appreciated

CODE
#include <GUIConstants.au3>

#include <Misc.au3>

Opt("TrayIconDebug",1)

dim $oldpos[2]

dim $pos[2]

HotKeySet("{Esc}","_Exit")

Dim $s_keys[117] = [116, _

"01","02","04","05","06","08","09","0C","0D","10", _

"11","12","13","14","1B","20","21","22","23","24", _

"25","26","27","28","29","2A","2B","2C","2D","2E", _

"30","31","32","33","34","35","36","37","38","39", _

"41","42","44","45","46","47","48","49","4A","4B","4C", _

"4D","4E","4F","50","51","52","53","54","55","56","57","58","59", _

"5A","5B","5C","60","61","62","63","64","65","66", _

"67","68","69","6A","6B","6C","6D","6E","6F","70", _

"71","72","73","74","75","76","77","78","79","7A", _

"7B","7C","7D","7E","7F","80H","81H","82H","83H","84H", _

"85H","86H","87H","90","91","A0","A1","A2","A3","A4","A5"]

$dll = DllOpen("user32.dll")

$pos = MouseGetPos()

$start = TimerInit()

While 1

Sleep ( 100 )

For $y = 1 To $s_keys[0]

If _IsPressed($s_keys[$y], $dll) Then

$start = TimerInit()

EndIf

Next

$oldpos[0] = $pos[0]

$oldpos[1] = $pos[1]

$pos = MouseGetPos()

if Not $oldpos[1] = $pos[1] Or not $oldpos[0] = $pos[0] Then

$start = TimerInit()

EndIf

if TimerDiff($start) > 600000 Then

MsgBox(0,"Inactive","You have not used keyboard or mouse for 10 minutes")

$start = TimerInit()

Else

EndIf

WEnd

Func _Exit()

DllClose($dll)

Exit

EndFunc

Share this post


Link to post
Share on other sites
JustinReno

10% CPU isn't that much, but if you want; you can use Wouter's reduce memory script:

Right before your while 1 line put _ReduceMemory()

CODE

Func _ReduceMemory($i_PID = -1)

If $i_PID <> -1 Then

Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID)

Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0])

DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0])

Else

Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1)

EndIf

Return $ai_Return[0]

EndFunc ;==>_ReduceMemory

Share this post


Link to post
Share on other sites
Siao

;struct to receive GetLastInputInfo() data.
Global $tagLASTINPUTINFO = DllStructCreate("uint; dword")
DllStructSetData($tagLASTINPUTINFO, 1, 8)
Global $pLASTINPUTINFO = DllStructGetPtr($tagLASTINPUTINFO)

While 1
    Sleep(5000) ;adjust to your preference
    $n = _GetLastInput()
    If $n > 600000 Then MsgBox(0, "Inactive", "You have not used keyboard or mouse for 10 minutes")
WEnd

Func _GetLastInput()
    If (IsDeclared('pLASTINPUTINFO') <> 1) Or (IsDeclared('tagLASTINPUTINFO') <> 1) Then Return -1
    $aRet = DllCall('user32.dll', 'int', 'GetLastInputInfo', 'ptr', $pLASTINPUTINFO)
    If $aRet[0] = 0 Then Return -1
    $aTick = DllCall('kernel32.dll', 'dword', 'GetTickCount')
    Return $aTick[0] - DllStructGetData($tagLASTINPUTINFO, 2)
EndFunc


"be smart, drink your wine"

Share this post


Link to post
Share on other sites
JustinReno

But then then it wouldn't be very accurate, The Sleep(5000) slows it down. You could move the mouse and press a key before the 5 seconds are over, which means your could trick it.

Share this post


Link to post
Share on other sites
Siao

which means your could trick it.

No. You just misunderstand how it works.

Anyway, I set it to 5 seconds because compared to 10 mins, it doesn't seem to be a big deal that you'll get notified approximately after 10min 5 seconds instead of 10min sharp, in my opinion. That's why I said adjust to your preference. Although I really don't see the point of setting it below 1000 miliseconds, considering that CPU consumption is a priority for topic starter.

But you can set it to whatever you wish.

Edited by Siao

"be smart, drink your wine"

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.