Jump to content

close only due to inactivity


Recommended Posts

#include <Timers.au3>
#include <FileConstants.au3>

Global Const $IDLE_MINUTES = 1, $TIME_WAITING = 10, $TIME_DESC = Not Mod ($TIME_WAITING, 60) ? "Minutes" : "Seconds"
Global Const $sProductFilePath = @WindowsDir
Global Const $sProductFileName = "RoyalTS.exe"
Global Const $sProductName = FileGetVersion($sProductFilePath & "\" & $sProductFileName, $FV_INTERNALNAME)

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()
    If TimerDiff($iTimerInit) >= $IDLE_MINUTES * 60 * 1000 Or $iIdleTime >= $IDLE_MINUTES * 60 * 1000 Then
      $idMsgBox = MsgBox(48, $sProductName, $sProductName & " has been idle for more than " & $IDLE_MINUTES & " minutes and will be closed in " & _
          $TIME_WAITING & " " & $TIME_DESC & " if no activity is detected.", $TIME_WAITING)
      If $idMsgBox = -1 Then
        ProcessClose($sProductFileName)
      Else
        $iTimerInit = TimerInit()
      EndIf
    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

Link to comment
Share on other sites

10 minutes ago, cesarzevil said:

I put this code, okay?

The code was published by @Nine in his contribution close-idle-application . I am sure it is ok ;)

@Nine BTW : autoclose-windows-application-after-45-minutes-of-inactivity

There is a little typo here (probably as a result of copy & paste)

#include <Timers.au3>

not

include <Timers.au3>

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

3 hours ago, cesarzevil said:

Sorry, I have no idea of programming and that's why I copied it ... It helped me a lot, so thanks @Nine

There is no need to apologize.
We are all learners, and always will be. The only difference is, how long someone has been traveling this road :).

Also using code created by others is not an issue - that' s why this community exists. However, it is always a nice gesture to thank the originator (as you did). You could also use the heart-shaped icon in the lower right corner of a contribution (e.g. the one from @Nine) to leave a reaction.

Aside : :welcome: to the Forum.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

On 6/5/2021 at 6:34 PM, cesarzevil said:

One question that I have, when I minimize the program but I am in another program after 1 minute it comes out, is there any way that the program does not exit even though it is with another program?

Unfortunately, it's not entirely clear to me what you intend 🤔.

On 6/5/2021 at 6:35 PM, cesarzevil said:

I am interested that by not touching the keyboard and the mouse for 1 minute it closes itself

This is exactly what @Nine 's script does. For completeness, here again the example script that monitors Notepad.exe :

Spoiler
#include <Timers.au3>
#include <FileConstants.au3>

HotKeySet("{ESC}", "_Terminate")

Global Const $IDLE_MINUTES = 1, $TIME_WAITING = 10, $TIME_DESC = Not Mod($TIME_WAITING, 60) ? "Minutes" : "Seconds"
Global Const $sProductFilePath = @WindowsDir
Global Const $sProductFileName = "Notepad.exe"
Global Const $sProductName = FileGetVersion($sProductFilePath & "\" & $sProductFileName, $FV_INTERNALNAME)

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()
        If TimerDiff($iTimerInit) >= $IDLE_MINUTES * 60 * 1000 Or $iIdleTime >= $IDLE_MINUTES * 60 * 1000 Then
            $idMsgBox = MsgBox(48, $sProductName, $sProductName & " has been idle for more than " & $IDLE_MINUTES & " minutes and will be closed in " & _
                    $TIME_WAITING & " " & $TIME_DESC & " if no activity is detected.", $TIME_WAITING)
            If $idMsgBox = -1 Then
                ProcessClose($sProductFileName)
            Else
                $iTimerInit = TimerInit()
            EndIf
        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

Func _Terminate()
    ConsoleWrite("==> Terminated" & @CRLF)
    Exit
EndFunc   ;==>_Terminate

 

Test scenario :
- start and minimize Notepad.exe
- start the monitor script
- now start another application, e.g. Wordpad
- make text inputs and/or mouse movements within Wordpad.

When the waiting period is over, the monitor script reports that Notepad is idle and closes it (how it should be).

Perhaps you could describe your requirement in more detail.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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