Jump to content

[Solved] Help with Inactivity Application


Rhys_A
 Share

Recommended Posts

Hi all, 

I'm fairly new to AutoIT (& scripting in general) - 

However, I was wondering if someone could tell me where I'm going wrong with this script, I am trying to write a application that closes a specific application after 15 minutes of inactivity with a 2 minute warning beforehand.

The times in the script are there for testing purposes;

Quote

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Global $Config = "C:\Users\--\Desktop\INI\Config.ini"
Global $BooleanMessageDisplayed = IniRead($Config, "Config", "BooleanMessageDisplayed", 0)
Global $InactivityTime = IniRead($Config, "Config", "InactivityTime", 30000)
Global $WarningMessageTime = $InactivityTime - 10000
Global $MessageBoxTimeOut = IniRead($Config, "Config", "MessageBoxTimeOut", 10)


While 1

    If _Timer_GetIdleTime() > $WarningMessageTime Then
        If Not $BooleanMessageDisplayed = 0 Then
            $BooleanMessageDisplayed = 0
        EndIf
    EndIf
    If _Timer_GetIdleTime() >= $InactivityTime Then
        WinExists("[CLASS:OMain]")
        CloseBoxTop()
    EndIf

WEnd

Func CloseBoxTop()
    WinExists("[CLASS:OMain]")
    WinClose("[TITLE:BoxTop Advantage]", "")
    ControlClick("[CLASS:#32770]", "&Yes", 6, "left", 1)
    $BooleanMessageDisplayed = 1
EndFunc   ;==>CloseBoxTop


Func MessageBoxTimeOut()
    #Region
    ;MsgBox features: Title=Yes, Text=Yes, Buttons=OK, Icon=Question, Timeout=50 ss
    WinExists("[CLASS:OMain]")
    If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox($MB_OK + $MB_ICONQUESTION, "Insufficient Activity", "BoxTop will close in 2 minutes unless there is user Input, or OK is pressed.", $MessageBoxTimeOut)
    If $iMsgBoxAnswer = 1 Then
        $BooleanMessageDisplayed = 0
    Else
        $BooleanMessageDisplayed = 1
    EndIf
    #EndRegion
EndFunc   ;==>MessageBoxTimeOut
 

 

 

Any help would be greatly appreciated

Kind Regards,

R

Edited by Rhys_A
Updating title
Link to comment
Share on other sites

Anyhelp about what ? 

What is the problem ?

I can see some wrong things in your script but what is the issue actually?

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

The message box interrupts the script, are you ok with a different type of notification?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Would something like this work for you?

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Opt("GUIOnEventMode", 1)
Opt("GUIEventOptions", 1)
Opt("WinTitleMatchMode", 3) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
Global $hGUI, $Window = 'Desktop';"[CLASS:OMain]"
Global $BooleanMessageDisplayed = 0, $MessageBoxTimeOut = 10
;Global $Config = 'C:\Users\&'@UserName'&\Desktop\INI\Config.ini'
;Global $BooleanMessageDisplayed = IniRead($Config, "Config", "BooleanMessageDisplayed", 0)
Global $InactivityTime = 10000 ;IniRead($Config, "Config", "InactivityTime", 30000)
Global $WarningMessageTime = $InactivityTime - 5000
;Global $MessageBoxTimeOut = IniRead($Config, "Config", "MessageBoxTimeOut", 10)

While 1
    $TimerIdle = _Timer_GetIdleTime()
    If $TimerIdle > $WarningMessageTime Then
        If WinExists($Window) = 1 And WinExists('Insufficient Activity') = 0 Then
        $hGUI = GUICreate('Insufficient Activity', 300, 100, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
        GUICtrlCreateLabel("BoxTop will close in 2 minutes unless there is user Input", 10, 10, 285, 25)
        GUISetState(@SW_SHOW, $hGUI)
        EndIf
    EndIf
    If $TimerIdle >= $InactivityTime Then
        If WinExists($Window) Then
            WinClose($Window)
            GUIDelete($hGUI)
            Exit
        EndIf
    EndIf
    If $TimerIdle = 0 Then
        If WinExists($hGUI) Then
        GUIDelete($hGUI)
        EndIf
    EndIf
    ConsoleWrite($TimerIdle &@CRLF&WinExists('Insufficient Activity')&@CRLF)
    Sleep(100)
WEnd

Func CloseMsg()
    GUIDelete($hGUI)
EndFunc   ;==>MessageBoxTimeOut

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Or this :

Global $Config = "C:\Users\--\Desktop\INI\Config.ini"
Global $InactivityTime = IniRead($Config, "Config", "InactivityTime", 30000)
Global $WarningMessageTime = $InactivityTime - 10000
Global $MessageBoxTimeOut = IniRead($Config, "Config", "MessageBoxTimeOut", 10)

Global $BooleanMessageDisplayed = False ; I do not see a reason to have this in .ini

While True
    If _Timer_GetIdleTime() >= $WarningMessageTime Then
        If Not $BooleanMessageDisplayed Then
            $BooleanMessageDisplayed = True
            MessageBoxTimeOut ()
        EndIf
    Endif
    If _Timer_GetIdleTime() >= $InactivityTime Then
        CloseBoxTop()
        ExitLoop 
    EndIf
WEnd

Func CloseBoxTop()
    WinClose("[TITLE:BoxTop Advantage]", "")
    ControlClick("[CLASS:#32770]", "&Yes", 6, "left", 1)
EndFunc   ;==>CloseBoxTop


Func MessageBoxTimeOut()
Local iMsgBoxAnswer

    $iMsgBoxAnswer = MsgBox($MB_OK + $MB_ICONQUESTION, "Insufficient Activity", "BoxTop will close in 2 minutes unless there is user Input, or OK is pressed.", $MessageBoxTimeOut)
    If $iMsgBoxAnswer = $IDOK Then $BooleanMessageDisplayed = False

EndFunc   ;==>MessageBoxTimeOut

 

Link to comment
Share on other sites

Hi - my problem was that the script, when run, would produce two msgbox's when the application was reopened and then close instantly again, this would happen whenever the application, in this case, BoxTop, was opened.

Thank you kindly for the responses.

 


Kind Regards,

Link to comment
Share on other sites

On 2/8/2019 at 8:39 PM, Nine said:

Or this :

Global $Config = "C:\Users\--\Desktop\INI\Config.ini"
Global $InactivityTime = IniRead($Config, "Config", "InactivityTime", 30000)
Global $WarningMessageTime = $InactivityTime - 10000
Global $MessageBoxTimeOut = IniRead($Config, "Config", "MessageBoxTimeOut", 10)

Global $BooleanMessageDisplayed = False ; I do not see a reason to have this in .ini

While True
    If _Timer_GetIdleTime() >= $WarningMessageTime Then
        If Not $BooleanMessageDisplayed Then
            $BooleanMessageDisplayed = True
            MessageBoxTimeOut ()
        EndIf
    Endif
    If _Timer_GetIdleTime() >= $InactivityTime Then
        CloseBoxTop()
        ExitLoop 
    EndIf
WEnd

Func CloseBoxTop()
    WinClose("[TITLE:BoxTop Advantage]", "")
    ControlClick("[CLASS:#32770]", "&Yes", 6, "left", 1)
EndFunc   ;==>CloseBoxTop


Func MessageBoxTimeOut()
Local iMsgBoxAnswer

    $iMsgBoxAnswer = MsgBox($MB_OK + $MB_ICONQUESTION, "Insufficient Activity", "BoxTop will close in 2 minutes unless there is user Input, or OK is pressed.", $MessageBoxTimeOut)
    If $iMsgBoxAnswer = $IDOK Then $BooleanMessageDisplayed = False

EndFunc   ;==>MessageBoxTimeOut

 

This works - however, the script stops completely when BoxTop closes - what would I need to input to keep your version of the script running even after Boxtop closes?

Kind Regards,

Rhys.

Link to comment
Share on other sites

12 minutes ago, iAmNewbe said:

Remove the ExitLoop after CloseBoxTop()

This will keep the While Loop running and your application will not exit after CloseBoxTop() runs

The script has to manage the fact that the inactivity period would continue after window closure.  The exitloop was a way to stop the script in checking inactivity. 

@Rhys_A you could use something like :

While True
    If not WinExists ("[TITLE:BoxTop Advantage]") then ; wait till application is back
        Sleep (1000)
        ContinueLoop
    Endif
    If _Timer_GetIdleTime() >= $WarningMessageTime Then
        If Not $BooleanMessageDisplayed Then
            $BooleanMessageDisplayed = True
            MessageBoxTimeOut ()
        EndIf
    Endif
    If _Timer_GetIdleTime() >= $InactivityTime Then CloseBoxTop()
    Sleep (100) ; reduce CPU impact
WEnd

 

Link to comment
Share on other sites

@Nine your update above is more inline with what the OP describes.   The first version of your code just exited once the window was closed and there was no way for any further checks to occur.

I took a stab at this and made a few changes to @Nines code.
 

Quote

Not $BooleanMessageDisplayed = 0

is equal to TRUE, so for readability I changed this to 

$BooleanMessageDisplayed = TRUE  , below.
 

While True
    WinWait("BoxTop Advantage"); wait till application is back        
    If _Timer_GetIdleTime() >= $WarningMessageTime Then
        If $BooleanMessageDisplayed = TRUE Then
           $BooleanMessageDisplayed = FALSE
           MessageBoxTimeOut()
        EndIf
    Endif
    If _Timer_GetIdleTime() >= $InactivityTime Then CloseBoxTop()
    Sleep (10) ; reduce CPU impact
WEnd

You can add a timeout to WinWait if you want, the script is essentially locked until the window reappears in this code snippit otherwise.

I don't like using NOT especially with booleans, just write it out in plain as possible language will remove confusion.

Link to comment
Share on other sites

Link to comment
Share on other sites

Hi all, 

Really appreciate the assist.

In the end we went with @Nine's way, but modified it slightly to remedy the CPU usage with sleep(10)'s.

I did other small edits with the functions and it's working perfectly - hoping to push out the script for our users soon.

:thumbsup:

Link to comment
Share on other sites

1 hour ago, Rhys_A said:

Hi all, 

Really appreciate the assist.

In the end we went with @Nine's way, but modified it slightly to remedy the CPU usage with sleep(10)'s.

I did other small edits with the functions and it's working perfectly - hoping to push out the script for our users soon.

:thumbsup:

Glad it works :)

Link to comment
Share on other sites

Cheers, 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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...