"While" Loop not honoring the conditional expression

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
FreedomReigns
Posts: 2
Joined: 21 Oct 2022, 17:47

"While" Loop not honoring the conditional expression

Post by FreedomReigns » 27 Nov 2022, 14:05

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.


^q::
n := 3
x := 0
sleep, 400
Label1:
sleep, 400
x := x + 1
WinActivate, All-In-One Virtual Event Platform | Zoom Events - Google Chrome ahk_class Chrome_WidgetWin_1
if n >= 6
{
sleep, 500
send, {esc}
sleep, 500
}
While (A_Index <= n)
{
sleep, 400 ; This works for n increases in +3 addition increments.
Send, {tab} ; The code works when n = 3 and 6 but keeps looping for n >=9, it never breaks per the expression
sleep, 400 ; I used msgbox to verify that the loop continued and also, I witnessed the continued send, {tab} behavior
} ; I tried just Loop {} with an "If" that breaks with A_Index = n and got the same thing, it keeps looping and
; ignores the If/Break block

send, {Enter}
sleep, 300
send, {alt}
loop, 4
{
sleep, 300
Send, {tab}
}
Sleep, 766
Send, {LControl Down}
Sleep, 500
Send, {c}
Sleep, 800
Send, {LControl Up}
sleep, 500
loop, 7
{
sleep, 500
Send, {Tab}
}
sleep, 500
Send, {Enter}
sleep, 500
WinActivate, Book2 - Excel ahk_class XLMAIN
sleep, 500
Send, {LControl Down}
Sleep, 500
send, {LShift Down}
sleep, 500
send, {U}
sleep, 800
Send, {LControl Up}
sleep, 800
send, {LShift Up}
sleep, 800
n := clipboard
;exitapp
Goto Label1
^k::exitapp

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

Re: "While" Loop not honoring the conditional expression

Post by mikeyww » 27 Nov 2022, 14:33

Welcome to this AutoHotkey forum!

Debugging this is straightforward. Inside your While loop, you can display the values of both A_Index and n. You currently have no idea what those values are. You will then have your answer quickly.

FYI, all letters are alphanumerically greater than all digits.

You can Loop instead of Goto.

The default maximum number of simultaneous threads per hotkey or hotstring is one.

#MaxThreadsPerHotkey:
If a hotkey has a max of 1 and it is pressed again while its subroutine is already running, the press will be ignored.
If n does not change inside your While loop or while the loop is active, then you can use Loop instead of While.

FreedomReigns
Posts: 2
Joined: 21 Oct 2022, 17:47

Re: "While" Loop not honoring the conditional expression

Post by FreedomReigns » 27 Nov 2022, 19:30

Hi Mikey and thank you sooo much for coming to my aid. I am sorry, but I must be missing something in translation. I responded to each of your sentences as quotations. Some background: I have been working with VBA for about 30 years and have been able to do some amazing things with it, but it's SendKey function is crazy timing finicky. My experience with AHK has been on and off over the past 3 years, where I successfully used it to hand shake 3 different application softwares and do crazy cool stuff, passing data through the clipboard, but I have never had so much trouble getting it to do what I want as of late. This is my first stab and using it between Excel and the Internet. I realize that I am still an still very much a newby with AHK and look forward to taking some classes on it. I apologize if my response comes across as rude or ridiculous, as I am sure some little thing that I am doing wrong is the thorn in hay here.
Debugging this is straightforward. Inside your While loop, you can display the values of both A_Index and n. You currently have no idea what those values are. You will then have your answer quickly.
As I stated in my first send, I explained that I used msgbox to display the values of each variable (n and I-index) for each pass through while loop and verified that the variables are as expected, but when the two variable values met the criteria for ending the loop, the loop did not exit. I know for a fact that the criteria as met, because I witnessed it with the msgbox output.

I_index correctly increments by 1 for each pass through the While loop, as verified with msgbox. The variable n increments by a value passed via the clipboard from an excel macro and it goes up by either 2 or 3 with each pass.Therefore the number of passes has no choice but to meet the criteria of ending the loop, as the variable for I_index change is by 1's and it is impossible to be jumped over, not to mention that I saw from the msgbox that the criteria was met.

I did the exact same thing with a Loop{...} by putting in in and "If (expression criteria) then break" control code and it did not break.

I am also noticing that on the second pass of the code, AHK is reading a Send {tab} for a Send {enter}. I interrupted between send {tabs} with a msgbox to verify that this was happening. On the second pass, the 3rd for 4 send {tabs} sent a send {enter}, because the response of the host app was not <tab> but was <enter>.

I am convinced that there must be a timing delay issue somehow. The host app is the Chrome browser. It may be the issue. I am not sure. I have been fighting this throughout my Thksgvng holiday and am frustrated.

"
If n does not change inside your While loop or while the loop is active, then you can use Loop instead of While.
"---- I tried the Loop {} with a conditional break inside but it did not "break" when the criteria was met between n and I_index, because the If block went unread. Is that due to it being a simultaneous thread?

"The default maximum number of simultaneous threads per hotkey or hotstring is one.
#MaxThreadsPerHotkey:
If a hotkey has a max of 1 and it is pressed again while its subroutine is already running, the press will be ignored."
--- Where in my code is there more than one thread running at once?

You can Loop instead of Goto.

Will a Loop instead of a GoTo at the bottom of the code help solve my problem?

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

Re: "While" Loop not honoring the conditional expression

Post by mikeyww » 27 Nov 2022, 20:16

So you did not post your debugging script that shows the problem, eh, or explain what the values actually were? :?

AutoHotkey is not broken. The entire issue can become visible with the following shorter script.

Code: Select all

n = 3
While (A_Index <= n)
{
 ToolTip, #%A_Index%# #%n%#
 Sleep, 400
 Send, {Tab}
 Sleep, 400
}
ToolTip
MsgBox, Done!
F3::n := 9
If you remain doubtful, you can use FileAppend to log the results to a file, so that you can go back to examine them.

The Windows clipboard is slow. Copying some text does not mean that the clipboard contains that text-- for a while. It could be a few milliseconds, or it could even take a second or so. This is almost certainly the mysterious delay that you are observing. The script that I posted will make that clear, and if you do some logging, it may be even more apparent.

After this short script leads to resolution of the problem, the script can be expanded again, and remaining issues examined.

Explained: ClipWait
AHK is reading a Send {tab} for a Send {enter}.
Means what? Check the KeyHistory.

While you test, close all other keyboard utilities, macro programs, AHK scripts, etc. Use no other code in your script, aside from logging.

Not only does your Excel macro copy text, but your original AHK script also copies text, right? So you really don't know what's on the clipboard at any moment-- unless you find out. You have two different programs copying potentially different text at the same time?

AutoHotkey can use COM to manipulate and work with Excel files. In many cases, it is more reliable.

Post Reply

Return to “Ask for Help (v1)”