Jump to content
Jarbydev

Autoclose Windows Application after 45 Minutes of inactivity

Recommended Posts

Jarbydev

I would to know how create a Script to do the following.

We have a windows Application and would like Autoit close the program after 45 minutes of inactivity, but before close the program generate a popup with the following message

"You have been idle for more than 45 Minutes, Program will close in 5 Seconds.

I would like to use the program title.

 

Thanks

Share this post


Link to post
Share on other sites
Subz

You can try something like the following:

#include <Array.au3>
#include <Timers.au3>
#include <WinAPIProc.au3>
Global $sProductFilePath = @WindowsDir
Global $sProductFileName = "Notepad.exe"
Global $sProductName = FileGetVersion($sProductFilePath & "\" & $sProductFileName, "ProductName")
Global $hProcessWnd, $aProcessList, $idMsgBox
Global $iTimerInit = TimerInit()
Global $iIdleTime = _Timer_GetIdleTime()
While 1
    $aProcessList = ProcessList($sProductFileName)
    If $aProcessList[0][0] >= 2 Then
        $idMsgBox = MsgBox(48, $sProductName, "Multiple Licenses Detected" & @CRLF & @CRLF & "Due to license limitations only one copy of " & $sProductName & " is allowed." & @CRLF & @CRLF & "Please close additional copies of " & $sProductName)
        $iTimerInit = TimerInit()
        ContinueLoop
    EndIf
    $iProcessId = ProcessExists($sProductFileName)
    If $iProcessId Then
        $iIdleTime = _Timer_GetIdleTime()
        $hProcessWnd = _GetHwndFromPID($iProcessId)
        If WinActive($hProcessWnd ) Then
            $iTimerInit = TimerInit()
        EndIf
        ;~ If the timer is more than 40 minutes then send message to user and close the process, otherwise it closes within 5 minutes
        If TimerDiff($iTimerInit) >= 40 * 60 * 1000 Or $iIdleTime >= 40 * 60 * 1000 Then
            $idMsgBox = MsgBox(48, $sProductName, $sProductName & " has been idle for more than 40 minutes and will be closed in 5 minutes if no activity is detected.", 300)
            If $idMsgBox = -1 Then ProcessClose($sProductFileName)
        EndIf
    EndIf
    Sleep(100)
WEnd

;Function for getting HWND from PID
Func _GetHwndFromPID($_iProcessId)
    Local $_hProcessWnd
    Local $iWinList, $_aWinList = WinList()
    For $i = 1 To $_aWinList[0][0]
        If $_aWinList[$i][0] <> "" Then
            $iWinList = WinGetProcess($_aWinList[$i][1])
            If $iWinList = $_iProcessId Then
                $_hProcessWnd = $_aWinList[$i][1]
                ExitLoop
            EndIf
        EndIf
    Next
    Return $_hProcessWnd
EndFunc;==>_GetHwndFromPID

 

  • Like 1

Share this post


Link to post
Share on other sites
Jarbydev

The Script works Perfectly, but the idmsgbox, is disappering in less of 2 seconds,

Share this post


Link to post
Share on other sites
Subz

Strange because it should stay open for 5 minutes (300 seconds), is it disappearing behind a Window?  You may need to add Top Most to the MsgBox i.e.

$idMsgBox = MsgBox(262144 + 48, $sProductName, $sProductName & " has been idle for more than 40 minutes and will be closed in 5 minutes if no activity is detected.", 300)

Assuming this is the MsgBox your referring to and not the Multiple Licenses Detected MsgBox.

Share this post


Link to post
Share on other sites
Jarbydev

Subz,

 

I double Checked and even minimized everthing and the popup appear for less of 2 seconds and then disappear after closing the notepad. i applied the change you suggested but not luck.

I tried also to run with a different program i have installed but this time the script doesn't close the app.

is possible use a different parameter like the title or the Class?

 

any help will be appreciated.

 

Thanks

 

Share this post


Link to post
Share on other sites
Subz

Can you tell me what the results of the script below are?  I've set it to 10 seconds for testing, I've also changed it from notepad.exe to cmd.exe, couple of other notes:

TimerInit checks if the Window is active
_Timer_GetIdleTime checks how long the machine has been idle, since a Window can still be "Active" even if your AFK.

Anyway the code below works fine for me, I used something similar a while ago before we moved to a license management system that handles this internally.  I'm not sure why the software wouldn't close for you, I've tried with a few different products and they all close for me, so thought I'd change it to cmd.exe to see if you get the same results.

#include <Array.au3>
#include <Timers.au3>
#include <WinAPIProc.au3>
Global $sProductFilePath = @SystemDir
Global $sProductFileName = "CMD.exe"
Global $sProductName = FileGetVersion($sProductFilePath & "\" & $sProductFileName, "ProductName")
Global $hProcessWnd, $aProcessList, $idMsgBox
Global $iTimerInit = TimerInit()
Global $iIdleTime = _Timer_GetIdleTime()
While 1
    $aProcessList = ProcessList($sProductFileName)
    If $aProcessList[0][0] >= 2 Then
        MsgBox(48, $sProductName, "Multiple Licenses Detected" & @CRLF & @CRLF & "Due to license limitations only one copy of " & $sProductName & " is allowed." & @CRLF & @CRLF & "Please close additional copies of " & $sProductName)
        $iTimerInit = TimerInit()
        $iIdleTime = _Timer_GetIdleTime()
        ContinueLoop
    EndIf
    $iProcessId = ProcessExists($sProductFileName)
    If $iProcessId Then
        $hProcessWnd = _GetHwndFromPID($iProcessId)
        If WinActive($hProcessWnd) Then
            $iTimerInit = TimerInit()
            $iIdleTime = _Timer_GetIdleTime()
        EndIf
        ;~ If the timer is more than 40 minutes then send message to user and close the process, otherwise it closes within 5 minutes
        If TimerDiff($iTimerInit) >= 10 * 1000 Or $iIdleTime >= 10 * 1000 Then
            $idMsgBox = MsgBox(48, $sProductName, $sProductName & " has been idle for more than 40 minutes and will be closed in 5 minutes if no activity is detected.", 10)
            If $idMsgBox = -1 Then ProcessClose($sProductFileName)
        EndIf
    EndIf
    Sleep(100)
WEnd

;Function for getting HWND from PID
Func _GetHwndFromPID($_iProcessId)
    Local $_hProcessWnd
    Local $iWinList, $_aWinList = WinList()
    For $i = 1 To $_aWinList[0][0]
        If $_aWinList[$i][0] <> "" Then
            $iWinList = WinGetProcess($_aWinList[$i][1])
            If $iWinList = $_iProcessId Then
                $_hProcessWnd = $_aWinList[$i][1]
                ExitLoop
            EndIf
        EndIf
    Next
    Return $_hProcessWnd
EndFunc;==>_GetHwndFromPID

 

Share this post


Link to post
Share on other sites
savu

Hi Subz, 

Great script. I've tested it and it works great. I have a problem with it in my environment:

I use a VM and several people connect remote desktop to it and so if the script is run by me it will also close off other peoples processes... this is not necessarily a bad thing but I would like to pass the pop-up error messages to their screen also. Do you have any clue on how to do that?

Regards,
Silviu

Share this post


Link to post
Share on other sites
Earthshine

you could start a new thread instead of feeding off an old one with your new "issue" which is not related to this script at all. You want to send windows messages to rdp users. Did you do a google search for an RDP UDF in autoit? I just searched and there does not look like any. You will have to learn about how to do this in Windows and then try to automate that process.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
Earthshine

AFIK you can do this from autoit app, from a command line.

msg * /SERVER:server_name Message goes here

and that will message everyone on that server when it happens. I am assuming this VM is a server with several users connected?


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
savu

Thank you Eartshine. 

Yes it's a server with several users connected.

regards,

Silviu

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

×