OnEvent has caused an infinite loop

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
ClearPhoenix
Newbie
Posts: 6
Joined: Tue Aug 30, 2016 1:15 pm

OnEvent has caused an infinite loop

Post by ClearPhoenix » Mon Nov 28, 2016 8:40 pm

Hi All;

I have a script that is intended to monitor inactivity. There is a specific remote program that is used on a manufacturing shop floor, and users have the tendency to never close the program on it as they are supposed to, to combat this, I've come up with a script to launch the program and then basically sit in a 30 minute wait, set a counter, then "while loop" on this counter until 6 loops occur.

While working out the bugs, I came to realize that if the end user actually did close the program themselves, the script would get caught, so I added an OnEvent to monitor for the specific process.

When the program is closed and the process terminated, the script becomes instantly stuck in the OnEvent>PROCESS_NOTEXISTS procedure, which makes logical sense, as the process doesn't exist anymore.

So, how do you make the OnEvent>PROCESS_NOTEXISTS only process through the procedure once?
I even included an Exit>0 in the SRT but it ignores it, and just keeps re-running the SRT assigned to the OnEvent trigger.

Full Script below.

Code: Select all

// this starts the kiosk program on startup rather than just using the rds shortcut.
Dialog>Dialog1
object Dialog1: TForm
  Left = 0
  Top = 0
  HelpContext = 5000
  BorderIcons = []
  BorderStyle = bsDialog
  Caption = 'Kiosk Inactivity Timer'
  ClientHeight = 210
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 12
    Top = 5
    Width = 569
    Height = 72
    Alignment = taCenter
    Caption =
      'There has been no activity on this computer for over 3 hours.'#13#10#13 +
      #10'This computer will close the kiosk and log off in:'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 240
    Top = 91
    Width = 113
    Height = 29
    Caption = '300 Seconds'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -24
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object MSButton1: tMSButton
    Left = 72
    Top = 157
    Width = 449
    Height = 47
    Caption = 'Press this button to cancel the logoff process'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    TabOrder = 8
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

GetCursorPos>oriX,oriY
MouseMove>6000,6000
GetCursorPos>maxX,maxY
MouseMove>oriX,oriY
let>centerX=%maxX%/2
let>centerY=%maxY%/2
GDP>Dialog1,,ClientHeight,dialogHeight
GDP>Dialog1,,ClientWidth,dialogWidth
let>dialogCenterX={(%centerX%-(%dialogWidth%/2))}
let>dialogCenterY={(%centerY%-(%dialogHeight%/2))}
SDP>Dialog1,,Left,%dialogCenterX%
SDP>Dialog1,,Top,%dialogCenterY%

ADH>Dialog1,,OnShow,getOpenTime
ADH>Dialog1,MSButton1,onClick,stopLogoff

SRT>getOpenTime
  VBEval>Now(),openTime
END>getOpenTime

SRT>stopLogoff
  Let>dialogStopped=1
  CloseDialog>Dialog1
END>stopLogoff

ReadIniFile>%SCRIPT_DIR%\KIOSK_TIMEOUT.ini,ResourceKey,ID,RES_KEY
ReadIniFile>%SCRIPT_DIR%\KIOSK_TIMEOUT.ini,InactivityLoop,waitLength,inactive
ReadIniFile>%SCRIPT_DIR%\KIOSK_TIMEOUT.ini,DialogTimeout,delayTime,timeout
Let>RDS_APPLICATION_LINK=C:\Users\%USER_NAME%\AppData\Roaming\Microsoft\Workspaces\%RES_KEY%\Resource\VTA Kiosk (AIL Apps).rdp
let>inactivityCounter=0
Let>dialogStopped=0

ProcessExists>mstsc.exe,RDS_RUNNING
IF>RDS_RUNNING=False
  RunProgram>%SYS_DIR%\mstsc.exe "%RDS_APPLICATION_LINK%"
  WaitProcessExists>mstsc.exe
Else
  Exit>0
EndIf

OnEvent>PROCESS_NOTEXISTS,mstsc.exe,0,UserLogoff

Label>resetLoop
While>inactivityCounter<=6
  GetCursorPos>nXPos_A,nYPos_A
  //Wait>%inactive%
  wait>300
  GetCursorPos>nXPos_B,nYPos_B
  IF>{(%nXPos_A%=%nXPos_B%) AND (%nYPos_A%=%nYPos_B%)}
    let>inactivityCounter=inactivityCounter+1
  ELSE
    let>inactivityCounter=0
  ENDIF
EndWhile

Let>dialogStopped=0
Let>delayTime=%timeout%
SDP>Dialog1,Label2,Caption,%delayTime% seconds
show>Dialog1
VBEval>Now(),currentTime
VBEval>datediff("s","%openTime%","%currentTime%"),elapsed
While>%elapsed%<=%delayTime%
  Let>captionTime=delayTime-elapsed
  Let>CaptionText=%captionTime% seconds...
  SDP>Dialog1,Label2,Caption,CaptionText
  if>%dialogStopped%=1
    // reset the timer loop....
    let>inactivityCounter=0
    goto>resetLoop
  endif
  VBEval>Now(),currentTime
  VBEval>datediff("s","%openTime%","%currentTime%"),elapsed
EndWhile
CloseDialog>Dialog1

// timer has expired, now have to find the "exit" button on the kiosk, wait until the RDS session closes, then log the computer off.
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  Wait>.5
  LClick
  Wait>1
  LClick
  Wait>1
  LClick
  GoSub>UserLogoff
Endif

SRT>UserLogoff
  //ShutDownWindows>2
  //Exit>0
  msg>end this
END>UserLogoff

ClearPhoenix
Newbie
Posts: 6
Joined: Tue Aug 30, 2016 1:15 pm

Re: OnEvent has caused an infinite loop

Post by ClearPhoenix » Tue Nov 29, 2016 1:39 pm

Well... sleeping on things as usual results in figuring out your own problem.

It dawned on me this morning that I missed the extremely important step of turning the OnEvent trigger off once you end up in the shutdown routine.

Full code below.

Code: Select all

// this starts the kiosk program on startup rather than just using the rds shortcut.
Dialog>Dialog1
object Dialog1: TForm
  Left = 0
  Top = 0
  HelpContext = 5000
  BorderIcons = []
  BorderStyle = bsDialog
  Caption = 'Kiosk Inactivity Timer'
  ClientHeight = 210
  ClientWidth = 600
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 12
    Top = 5
    Width = 569
    Height = 72
    Alignment = taCenter
    Caption =
      'There has been no activity on this computer for over 3 hours.'#13#10#13 +
      #10'This computer will close the kiosk and log off in:'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 240
    Top = 91
    Width = 113
    Height = 29
    Caption = '300 Seconds'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -24
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object MSButton1: tMSButton
    Left = 72
    Top = 157
    Width = 449
    Height = 47
    Caption = 'Press this button to cancel the logoff process'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -19
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    ParentFont = False
    TabOrder = 8
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1

GetCursorPos>oriX,oriY
MouseMove>6000,6000
GetCursorPos>maxX,maxY
MouseMove>oriX,oriY
let>centerX=%maxX%/2
let>centerY=%maxY%/2
GDP>Dialog1,,ClientHeight,dialogHeight
GDP>Dialog1,,ClientWidth,dialogWidth
let>dialogCenterX={(%centerX%-(%dialogWidth%/2))}
let>dialogCenterY={(%centerY%-(%dialogHeight%/2))}
SDP>Dialog1,,Left,%dialogCenterX%
SDP>Dialog1,,Top,%dialogCenterY%

ADH>Dialog1,,OnShow,getOpenTime
ADH>Dialog1,MSButton1,onClick,stopLogoff

SRT>getOpenTime
  VBEval>Now(),openTime
END>getOpenTime

SRT>stopLogoff
  Let>dialogStopped=1
  CloseDialog>Dialog1
END>stopLogoff

ReadIniFile>%SCRIPT_DIR%\KIOSK_TIMEOUT.ini,ResourceKey,ID,RES_KEY
ReadIniFile>%SCRIPT_DIR%\KIOSK_TIMEOUT.ini,InactivityLoop,waitLength,inactive
ReadIniFile>%SCRIPT_DIR%\KIOSK_TIMEOUT.ini,DialogTimeout,delayTime,timeout
Let>RDS_APPLICATION_LINK=C:\Users\%USER_NAME%\AppData\Roaming\Microsoft\Workspaces\%RES_KEY%\Resource\VTA Kiosk (AIL Apps).rdp
let>inactivityCounter=0
Let>dialogStopped=0

ProcessExists>mstsc.exe,RDS_RUNNING
IF>RDS_RUNNING=False
  RunProgram>%SYS_DIR%\mstsc.exe "%RDS_APPLICATION_LINK%"
  WaitProcessExists>mstsc.exe
Else
  Exit>0
EndIf

OnEvent>PROCESS_NOTEXISTS,mstsc.exe,,UserLogoff

Label>resetLoop
While>inactivityCounter<=6
  GetCursorPos>nXPos_A,nYPos_A
  Wait>%inactive%
  GetCursorPos>nXPos_B,nYPos_B
  IF>{(%nXPos_A%=%nXPos_B%) AND (%nYPos_A%=%nYPos_B%)}
    let>inactivityCounter=inactivityCounter+1
  ELSE
    let>inactivityCounter=0
  ENDIF
EndWhile

Let>dialogStopped=0
Let>delayTime=%timeout%
SDP>Dialog1,Label2,Caption,%delayTime% seconds
show>Dialog1
VBEval>Now(),currentTime
VBEval>datediff("s","%openTime%","%currentTime%"),elapsed
While>%elapsed%<=%delayTime%
  Let>captionTime=delayTime-elapsed
  Let>CaptionText=%captionTime% seconds...
  SDP>Dialog1,Label2,Caption,CaptionText
  if>%dialogStopped%=1
    // reset the timer loop....
    let>inactivityCounter=0
    goto>resetLoop
  endif
  VBEval>Now(),currentTime
  VBEval>datediff("s","%openTime%","%currentTime%"),elapsed
EndWhile
CloseDialog>Dialog1

// timer has expired, now have to find the "exit" button on the kiosk, wait until the RDS session closes, then log the computer off.
//Find and Left Click Center of 
FindImagePos>%BMP_DIR%\image_1.bmp,SCREEN,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
  MouseMove>XArr_0,YArr_0
  Wait>.5
  LClick
  Wait>1
  LClick
  Wait>1
  LClick
  GoSub>UserLogoff
Endif

SRT>UserLogoff
  OnEvent>PROCESS_NOTEXISTS,mstsc.exe,,
  ShutDownWindows>2
  Exit>0
END>UserLogoff

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts