Jump to content

Button control stops working after first use


Recommended Posts

I'm writing an autoit script for our computer labs. If a student forgets to logout, I want this script running in the background watching the idle time.

Once an idletime limit has been reached, popup a "You will be logged off in XX seconds" dialog. This would have a button to cancel the countdown and allow the student to return to work, just in case.

And the script I have works on the first iteration of the popup, but if I click the button to reset, on the next popup, the button does nothing and eventually the logout will happen.

Below is the code I have, I have a msgbox instead of the actual logout for now. The test setup is for 5 seconds of idle time and a 30 second countdown timer.

#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <Timers.au3>
Opt("GUIOnEventMode", 1)

Global $window = GUICreate("Forced Logoff ...", 200, 300,-1,-1)
GUICtrlCreateLabel("Logoff in...", 30, 10)
Global $lblCountdown = GUICtrlCreateLabel("30", 30, 30)
$btn_reset = GUICtrlCreateButton("Stay Logged In",50,50,100)
GUICtrlSetOnEvent($btn_reset, "myLogoffReset")


Global $iReset = 1
Global $idleLimit = 1000 * 5 


Func myGetIdleTime()
    Dim $idleLimit

    Local $idleTime = _Timer_GetIdleTime()

    While  $idleTime  < $idleLimit
        sleep(2 * 1000) ;pause for 2 seconds inbetween checks
        $idleTime = _Timer_GetIdleTime()
    WEnd
    myLogout()
EndFunc


Func myLogout()
    
    Dim $iReset = 0
    Local $iLogoutTime = 30
    GUICtrlSetData($lblCountdown,string($iLogoutTime))
    
    GUISetState(@SW_SHOW)


    
    while $iLogoutTime >= 0
        sleep (1000)
        if $iReset == 1 Then
            ExitLoop
        Else
            $iLogoutTime -= 1
            GUICtrlSetData($lblCountdown,$iLogoutTime)
        EndIf
        
    WEnd
    
    IF $iReset == 0 THEN
        MsgBox(0, "Logoff", "Logoff would happen now!")
    EndIf
EndFunc

Func myLogoffReset()
        Dim $iReset = 1
        GUISetState(@SW_HIDE)
        myGetIdleTime()
EndFunc

        
; start it up
myGetIdleTime()
Link to comment
Share on other sites

I think your main problem was here:

Func myLogoffReset()
        Dim $iReset = 1
        GUISetState(@SW_HIDE)
        myGetIdleTime()  ;<====== Problem
EndFunc

This was causing the program to call the myGetIdleTime() function multiple times invalidating the handles, or something like that. Here is how I got it working:

#include <WindowsConstants.au3>
#include <GuiConstantsEx.au3>
#include <Timers.au3>
Opt("GUIOnEventMode", 1)

Global $window = GUICreate("Forced Logoff ...", 200, 300,-1,-1)
GUICtrlCreateLabel("Logoff in...", 30, 10)
Global $lblCountdown = GUICtrlCreateLabel("30", 30, 30, 300, 20)
$btn_reset = GUICtrlCreateButton("Stay Logged In",50,60,100)
GUICtrlSetOnEvent($btn_reset, "myLogoffReset")


Global $iReset = 0
Global $idleLimit = 1000 * 5 


Func myGetIdleTime()
    
    While  1
    If _Timer_GetIdleTime()  > $idleLimit Then
        $idleTime = _Timer_GetIdleTime() ;Not sure if this is really needed, but left in case you use it elsewhere in the program
        myLogout()
    EndIf
        sleep(2 * 1000) ;pause for 2 seconds inbetween checks
    WEnd
    
EndFunc


Func myLogout()
    
    $iReset = 0
    Local $iLogoutTime = 30
    GUICtrlSetData($lblCountdown,Int($iLogoutTime))
    
    GUISetState(@SW_SHOW)
    
    while $iLogoutTime > 0
        sleep (1000)
        if $iReset = 1 Then
            Return
        Else
            $iLogoutTime -= 1
            GUICtrlSetData($lblCountdown,$iLogoutTime)
        EndIf
        
    WEnd
    
    If ($iReset = 0) THEN
    Exit
    EndIf
    
EndFunc

Func myLogoffReset()
    $iReset = 1
    GUISetState(@SW_HIDE)
EndFunc
        
; start it up
myGetIdleTime()

Hope this is helpful! :)

Link to comment
Share on other sites

The case reset function was just restarting the whole thing all the time, it didnt even go through here.

If $iReset = 1 Then

ExitLoop

Else

You could also just make an endless loop in which you would restart myGetIdleTime() and remove it from Func myLogoffReset()

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...