"Gui" stops working after 10 times

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

"Gui" stops working after 10 times

Post by DevWithCoffee » 21 Jul 2021, 23:10

Hi, thanks for the open space to ask questions.
I don't have much experience in creating an interface (Gui), I tried to create a password blocker to avoid using another existing program, but I removed the return to automatically close after 15 seconds (or almost).

It works 9 or 10 times, so the "Close", "Escape", "Send" buttons no longer work.

I assume it's my mistake, I made a simplified version to understand:

Code: Select all

#SingleInstance Force
exeTarget := "notepad.exe"
;CFG_INI := RegExReplace(A_ScriptName, "\.[^.]*$")
;MsgBox % CFG_INI
;IniWrite, main, %CFG_INI%.ini, Section, Value

Standby:
PWTry = 0
Loop
{
	IfWinExist, ahk_exe %exeTarget%
	{
		Break
	}
	Sleep 100
}

Authentic:
Gui,2: Destroy
Gui,2: -MinimizeBox +LastFound +Owner -dpiscale
Gui,2: Add, Edit, w120 h20 x8 y9 Password vPword
Gui,2: Add, Button, w32 h22 x132 y8 +Default gChkPW vVOK, OK
Gui,2: Show, w172 h38 Center, Your Password:, nohide

Loop, 400
{
	IfWinActive, ahk_exe %exeTarget%
	{
		; --------- Only Executable
		If A_IsCompiled
		{
			WinActivate, ahk_exe %A_ScriptName%
		}
		else
		{
			WinActivate, Your Password:
		}
		;GuiControl,2: Focus, VOK
		GuiControl,2: Text, VOK, "PWTry"
		GuiControl,2: Focus, Pword
	}
	Sleep, 10
}
Goto CloseExe

2GuiClose:
Goto CloseExe

2GuiEscape:
Goto CloseExe

ChkPW:
Gui,2: Submit
; --------- Sample Master Password 
If (Pword == "@lfa")
{
	Goto AllowedExe
}
If (PWTry > 20)
{
	Goto CloseExe
}
PWTry++
Goto Authentic

AllowedExe:
Gui,2: Destroy
Loop
{
	; --------- Wait Close
	IfWinNotExist, ahk_exe %exeTarget%
	{
		Break
	}
	Sleep 100
}
Goto Standby

CloseExe:
Gui,2: Destroy
WinClose, ahk_exe %exeTarget%
Goto Standby
Last edited by DevWithCoffee on 22 Jul 2021, 16:24, edited 1 time in total.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: "Gui" stops working after 10 times

Post by mikeyww » 22 Jul 2021, 07:23

Code: Select all

target   = Calculator
pwd      = @lpha
maxTries = 3
Gui, Font, s10
Gui, Add, Edit, w210 Password vtry
Gui, Add, Button, x+m Default, OK
Loop {
 WinWait, %target%
 If (try != pwd) {
  WinClose
  PWTry = 0
  Gui, Show,, Password for Calc
 } Else WinWaitClose
}
ButtonOK:
Gui, Submit, NoHide
GuiControl,, try
PWTry++
If (try = pwd) {
 Gui, Hide
 MsgBox, 64, Success, You are authorized.
 Run, Calc
 Return
} Else If (PWTry < maxTries)
 Return
GuiClose:
GuiEscape:
Gui, Hide
Return
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: "Gui" stops working after 10 times

Post by DevWithCoffee » 22 Jul 2021, 16:20

Thanks for replying, I didn't understand your example well, I edited my code because there were some errors. I also noticed that the same problem happens if I hit the close button or hit the escape button 10 times in a row.
I removed the "return" just so that the "GUI" was destroyed after a few seconds.

But if there is a limitation for creating a "GUI" on the screen, I'll put it that it can only be invoked by the "trayicon"
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: "Gui" stops working after 10 times

Post by mikeyww » 22 Jul 2021, 17:18

There are no such limits.

According to what you programmed, if the target window does not become active within four seconds, the script and window will be closed. You can add MsgBox lines to show what is happening and to verify that. Follow the script line by line. "Goto" has a tendency towards errors in coding. Using WinWait also simplifies the script and makes it more efficient at the same time.
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: "Gui" stops working after 10 times

Post by DevWithCoffee » 22 Jul 2021, 18:28

mikeyww wrote:
22 Jul 2021, 17:18
There are no such limits.
I tried to simplify the problem as much as possible:

Code: Select all

#SingleInstance force
UsageTime = 0
ChkTry = 0

startGui:
Gui, New, -MinimizeBox +LastFound +Owner, Confirm:
Gui, Add, Text, x6 y8, Usage time: %UsageTime% seconds
Gui, Add, Button, w32 h22 x132 y8 +Default gChkPW, OK
Gui, Show, w172 h38 Center
;return ;If you want to stop the Loop

Loop
{
  ;======== Parallel process
  Sleep, 800
  UsageTime++
}

ChkPW:
Gui, Submit
ChkTry++
MsgBox, Number of executions: %ChkTry%

GuiClose:
GuiEscape:
Goto startGui
mikeyww wrote:
22 Jul 2021, 17:18
You can add MsgBox lines to show what is happening and to verify that. Follow the script line by line.
I tried it, I even checked the possibilities of some loop that wasn't broken and was in parallel with another one.
mikeyww wrote:
22 Jul 2021, 17:18
"Goto" has a tendency towards errors in coding. Using WinWait also simplifies the script and makes it more efficient at the same time.
Should I use this to check which window?

Thnx
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: "Gui" stops working after 10 times

Post by mikeyww » 22 Jul 2021, 21:51

This flow looks problematic to me, because you will end up with multiple loops. I don't think I can add much to the working script and the comments that I have provided so far. To work with your own script, you would need to go through it line by line, to follow the flow and each command. Your original script can work with some adjustments. If you display a MsgBox with the result of each conditional statement after it is checked, it may help you to track down the bugs. As I mentioned, one of your loops closes the target window after four seconds if it is not activated.
User avatar
DevWithCoffee
Posts: 54
Joined: 13 Oct 2020, 12:16

Re: "Gui" stops working after 10 times

Post by DevWithCoffee » 23 Jul 2021, 07:38

Thank you very much, I think I managed to solve it.
A member on a "Board" that speaks another language showed the solution with settimer, so I don't need to remove the "return" and the problem doesn't happen.

As I assumed the "return" is there for some important reason.

Code: Select all

Gui, Show, w400 h100 Center,Confirm:
settimer, MyLabel,1000
return

MyLabel
;parallel process
return
Post Reply

Return to “Ask for Help (v1)”