Jump to content

Closing an idle process after a certain period of time


Recommended Posts

Hello,

I have posted this to an old topic but got no response, so I thought maybe I should start a new one.

I have been working on it solve the problem my company has with an ERP software. There are limited number of licenses and people just leave the software open logged in which prevents others to use it. So, I've wanted to automatically close the software on user PCs after a certain period of being idle. 

The problem I have; the ERP software obviously predicted people would try this so they have a way of not logging the user off when the software is closed from task manager. Or using ProcessClose/WinKill commands. The only way to close the software and successfully log out is by pressing the "X" on the window and then clicking "Yes" in the pop-up window. Pressing "e" should also work. ChatGPT suggested using WinClose with exact window name. I can go as far as the little pop-up window but then things go haywire. Either it can't click "Yes" and the pop-up window stuck or somehow all the menus disappear on the window and I have to use the task manager to close the software. 

I'm attaching the code I have used. I'm also open to other ideas and maybe there is a different command which would force close the software meanwhile logging it off. Thanks in advance.

 

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

Global Const $IDLE_MINUTES = 1
Global Const $sWindowTitle = "ERP Software"

Global $hProcessWnd, $aProcessList, $sWindowClass
Global $iTimerInit = TimerInit()
Global $iIdleTime = _Timer_GetIdleTime()

; Disable Pause hotkey (Ctrl+Pause) to prevent users from pausing the script
HotKeySet("^{Pause}", "NoPause")

; Hide the tray icon
#NoTrayIcon

Func NoPause()
    ; Do nothing
EndFunc

While 1
    $hProcessWnd = WinGetHandle($sWindowTitle)
    If $hProcessWnd <> 0 Then
        $iIdleTime = _Timer_GetIdleTime()
        $sWindowClass = WinGetClassList($hProcessWnd)
        If WinActive($hProcessWnd) Then $iTimerInit = TimerInit()
        If TimerDiff($iTimerInit) >= $IDLE_MINUTES * 60 * 1000 Or $iIdleTime >= $IDLE_MINUTES * 60 * 1000 Then
            ; Forcefully close the main window
            WinKill($hProcessWnd)

            ; Wait for the small window to appear (adjust the sleep time if needed)
            Sleep(1000)

            ; Send "Enter" key to the small window to select the default option ("Yes")
            Send("{ENTER}")
        EndIf
    EndIf

    Sleep(100)
WEnd

 

Link to comment
Share on other sites

  • Developers

So you probably require a real logoff to be performed by the software. Can't you simulate that?

For starters I would try WinClose() as that should simulate the clicking the X. ( Assuming that is what normally is required for the license to be released?)

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

15 hours ago, Jos said:

So you probably require a real logoff to be performed by the software. Can't you simulate that?

For starters I would try WinClose() as that should simulate the clicking the X. ( Assuming that is what normally is required for the license to be released?)

I've tried that too and I'm attaching the code. But for some reason it fails to press "yes" successfully in the pop-up window after clicking "X" on the main window.

Btw, the pop-up window asks "are you certain you want to close the software?" and there are three options for clicking: yes, no and cancel. 

 

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

Global Const $IDLE_MINUTES = 1
Global Const $sWindowTitle = "ERP Software"

Global $hProcessWnd, $aProcessList, $sWindowClass
Global $iTimerInit = TimerInit()
Global $iIdleTime = _Timer_GetIdleTime()

; Disable Pause hotkey (Ctrl+Pause) to prevent users from pausing the script
HotKeySet("^{Pause}", "NoPause")

; Hide the tray icon
#NoTrayIcon

Func NoPause()
    ; Do nothing
EndFunc

While 1
    $hProcessWnd = WinGetHandle($sWindowTitle)
    If $hProcessWnd <> 0 Then
        $iIdleTime = _Timer_GetIdleTime()
        $sWindowClass = WinGetClassList($hProcessWnd)
        If WinActive($hProcessWnd) Then $iTimerInit = TimerInit()
        If TimerDiff($iTimerInit) >= $IDLE_MINUTES * 60 * 1000 Or $iIdleTime >= $IDLE_MINUTES * 60 * 1000 Then
            ; Close the main window gracefully
            WinClose($hProcessWnd)

            ; Wait for the small window to appear (adjust the sleep time if needed)
            Sleep(1000)

            ; Send "Enter" key to the small window to select the default option ("Yes")
            Send("{ENTER}")
        EndIf
    EndIf

    Sleep(100)
WEnd

 

Edited by redvern
Link to comment
Share on other sites

1 hour ago, Jos said:

Are you sure the Yes button has the Focus when you Send("{Enter}")?

 

short answer, I don't know. normally when I manually click "X" then I can press "enter" or "e" with hand to close the software. the focus is on the "yes" button.

but I don't know what exactly happens when the script is running. It looks like either it can't press any button or for some reason it breaks the UI and the menus dissapper one by one. 

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