Jump to content
Sign in to follow this  
KaFu

__Timer_IdleChecker()

Recommended Posts

KaFu

HiHo,

in course of investigating how to use SMF's idle time for background update activity I came up with the following solution.

The program is considered idle after x ms of inactivity (mouse AND keyboard) OR if none of the program's windows are active.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
#include <WinAPI.au3>

Local $msg
Global $bIdle = false
Global $iIdle_Time = 10000 ; program considered idle after x ms of inactivity (mouse AND keyboard) OR if none of the programs windows are active

$hGUI = GUICreate("Idle Time Test-GUI",300,100,100,100,Default,$WS_EX_TOPMOST)
$label = GUICtrlCreateLabel("NOT Idle...",10,10,200)
$label2 = GUICtrlCreateLabel("",10,40,200,20)
GUISetState(@SW_SHOW)

$hTimer_IdleCheck = _Timer_SetTimer($hGUI, 1000, "__Timer_IdleChecker") ; check if program is idle / NOT idle every x ms...

$hGUIchild = GUICreate("Idle Time Test-GUI Child",300,100,500,100,Default,$WS_EX_TOPMOST) ; child GUI to demonstrate check of none of the programs windows are active
GUISetState(@SW_SHOW)

While 1
   
    sleep(10)

    $msg = GUIGetMsg()

    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
   
    if $bIdle = true Then
        GUICtrlSetData($label, "Idle...")
        while $bIdle = true
            ; Do something if idle...
            ; here be more code
            ; ...
            sleep(100)
            ; for more complex / lengthly operations / loops insert something like if $bIdle = true then exitloop
            GUICtrlSetData($label2,TimerInit())
        wend
        GUICtrlSetData($label, "NOT Idle...")
    endif
   
WEnd

GUIDelete()

func __Timer_IdleChecker($hWnd, $Msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
   
    if @AutoItPID <> WinGetProcess(_WinAPI_GetAncestor(WinGetHandle("[ACTIVE]"), $GA_ROOTOWNER)) then ; none of the programs windows are active
        $bIdle = true
    Else
        if _Timer_GetIdleTime() > $iIdle_Time Then ; program considered idle after x ms of inactivity
            $bIdle = True
        Else
            $bIdle = False
        endif
    endif
EndFunc

Maybe useful to someone else, or maybe someone has ideas how to improve this one :P...

Best Regards

Edited by KaFu

Share this post


Link to post
Share on other sites
bhyde1984

This is an incredible script! I'm blown away that no one has commented so I signed up for an account just to comment on this posting!

I modified the script slightly to fit my needs but this thing is great! I wrote a script that disables the wireless network adapter if the wired adapter is active but had issues with it pulling CPU usage all the time. This script was the perfect fix! I worked it into my existing script and now if the mouse and/or keyboard don't move for 1 second, then the script pauses. It's great!

Thanks again for the great posting!

Share this post


Link to post
Share on other sites
KaFu

Thanks for the nice feedback :D...

In my current script I embed some long running functions in this

_Timer_KillTimer($gui_SMF_Search_Main, $gui_SMF_Search_Main_Timer_IdleCheck)
;.... long running function
$gui_SMF_Search_Main_Timer_IdleCheck = _Timer_SetTimer($gui_SMF_Search_Main, $iIdle_CheckTime, "__Timer_IdleChecker")

to deactivate the check while running, but otherwise I didn't found any further improvement possibilities yet. What did you tweak?

Best Regards

Edited by KaFu

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  

×