modify setbatchlines

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
marypoppins_1
Posts: 127
Joined: 28 Oct 2020, 02:58

modify setbatchlines

24 Mar 2024, 02:15

hello. i was reading on the changes from v1 to v2 and i saw that v2 now defaults to setbatchlines -1 and that the function (or command) is now deleted. is there a way to modify how much (in ms) to wait after executing every line? an approach that's not adding sleep after every line. also i want this to be specific to one script not all. (one script can have 5ms delay the other 10ms) any idea??
vmech
Posts: 362
Joined: 25 Aug 2019, 13:03

Re: modify setbatchlines

24 Mar 2024, 02:39

@marypoppins_1
Adding delays (e.g. Sleep) not after every lines, but into critical code's places, is only a way in v2.
Please post your script code inside [code] ... [/code] block. Thank you.
User avatar
boiler
Posts: 17295
Joined: 21 Dec 2014, 02:44

Re: modify setbatchlines

24 Mar 2024, 05:15

marypoppins_1 wrote: is there a way to modify how much (in ms) to wait after executing every line?…(one script can have 5ms delay the other 10ms) any idea??
Even in v1, there wasn’t a way to modify how long it waits/sleeps after every line. The sleep period was always 10 ms. You could specify how much run time should elapse before sleeping for 10 ms or how many lines to execute before sleeping for 10 ms.

In v1, SetBatchLines 5ms would cause it to sleep for 10 ms after it has run for 5 ms no matter how many lines are executed in that time. To have it sleep after every line, you would specify SetBatchLines 1, which would cause it to sleep for 10 ms after every line. You can’t mix the two, and in neither case is the sleep duration being altered.

If you try to simulate this by adding a Sleep of your own between other code lines, note that delays of less than 10 ms are typically rounded up to the nearest 10 ms. To achieve a delay of 5 ms, you would need to implement something similar to this example (#3).
marypoppins_1
Posts: 127
Joined: 28 Oct 2020, 02:58

Re: modify setbatchlines

24 Mar 2024, 11:02

@boiler okay very interesting. thank you for clarifying. this has raised several questions in my mind.
first, at what speed precisely(or roughly) does ahk take to go from one line of code to another? is it in ms or microseconds?
second, is it easy to find out if my pc rounds to the nearest 10 / 15.6? (where do these numbers come from anyway?)
third, i'm doing this because i have a hotkey that launches a website (waits for the page to load) and fills in my personal data. i automated the process with imagesearch but even in the lines of code before reaching the imagesearch, i feel the computer is overwhelmed or bugs; hence, there are a lot of "sleeps" in the code. i can post the code if you want. my concern is: should i be using pixelsearch or imagesearch is fine? also is it possible to quickly skim over my code to check whether i have something off?
User avatar
boiler
Posts: 17295
Joined: 21 Dec 2014, 02:44

Re: modify setbatchlines

24 Mar 2024, 11:29

marypoppins_1 wrote: first, at what speed precisely(or roughly) does ahk take to go from one line of code to another? is it in ms or microseconds?
It is on the sub-ms scale, which can be seen if you run this code, showing 0 ms for elapsed time:

Code: Select all

#Requires AutoHotkey 2.0
StartTime := A_TickCount
Dummy := 1
MsgBox "Elapsed time: " A_TickCount - StartTime

marypoppins_1 wrote: second, is it easy to find out if my pc rounds to the nearest 10 / 15.6? (where do these numbers come from anyway?)
You should be able to see by running this version, which consistently shows either 15 or 16 for me:

Code: Select all

#Requires AutoHotkey 2.0
StartTime := A_TickCount
Sleep 5
MsgBox "Elapsed time: " A_TickCount - StartTime

marypoppins_1 wrote: third, i'm doing this because i have a hotkey that launches a website (waits for the page to load) and fills in my personal data. i automated the process with imagesearch but even in the lines of code before reaching the imagesearch, i feel the computer is overwhelmed or bugs; hence, there are a lot of "sleeps" in the code. i can post the code if you want. my concern is: should i be using pixelsearch or imagesearch is fine? also is it possible to quickly skim over my code to check whether i have something off?
You can post the code if you want. Whether to use PixelSearch or ImageSearch should be based on whichever is best suited for your use case. If you're looking for an image, use ImageSearch. Waiting for a page to load is something that you should use some positive identification for as opposed to sleeping for some specific amount of time, and you definitely should not be trying to handle things like that with default sleep after every line as v1 allowed.
vmech
Posts: 362
Joined: 25 Aug 2019, 13:03

Re: modify setbatchlines

24 Mar 2024, 12:04

marypoppins_1 wrote:
24 Mar 2024, 11:02
first, at what speed precisely(or roughly) does ahk take to go from one line of code to another? is it in ms or microseconds?
It depends on the code itself.
For example, a function with a really large number of relatively simple mathematical operations, even with loops, will be executed very quickly - units or tens of microseconds.
But DllCall calls or searching for pixels/images are quite «heavy», so they will take much longer (by orders of magnitude).
marypoppins_1 wrote:
24 Mar 2024, 11:02
second, is it easy to find out if my pc rounds to the nearest 10 / 15.6? (where do these numbers come from anyway?)
This time period is called a timeslice, and is determined by the OS. Its typical value is 10 ms for Windows 7, and 15.625 ms for Windows 8/10/11.
Please post your script code inside [code] ... [/code] block. Thank you.
marypoppins_1
Posts: 127
Joined: 28 Oct 2020, 02:58

Re: modify setbatchlines

25 Mar 2024, 00:48

okay
1: i tested it and got 0ms consistently (because of the rounding). i found a quickperformancecounter function utilizing dll calls in the documentation, so i used it. result was roughly 1 microsecond without using a sleep (computer resources weren't heavily used but i'll try that later).
2: the first time i ran it i got 47, then a consistent 15 or 16. but does that mean it rounds to the nearest multiple of 16?
3: i will send my code with all the sleeps commented out. the reason i added them is not to wait for the site to load (because what if the wifi was slow / site is down / ...) i added them because the ahk script bugged (either the current msedge tab closed, or screen went all black). so just to give the script time to breathe. btw the thing that bugs (as far as i know) is everything before the imagesearch
thanks in advance @boiler

Code: Select all

^Numpad1:: { ; Hotkey 
	b2ba := CredRead("b2b") ; custom function to read windows credentials
    If !WinExist("ahk_exe msedge.exe"){ ; If the process "msedge.exe" doesn't exist, proceed
        ; Sleep(100)
        Run("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe") ; Will run Edge if it isn't already running
        ; Sleep(100)
        WinWait("ahk_class Chrome_WidgetWin_1 ahk_exe msedge.exe") ; Waits till edge fully loads
        ; Sleep(250)
        WinActivate("ahk_exe msedge.exe") ; Activates edge window
        ; Sleep(100)
        WinWaitActive("ahk_exe msedge.exe")
    }
    If (WinExist("ahk_exe msedge.exe")){ ; If the process "msedge.exe" does exist, proceed 
        ; Sleep(100)
        if !(WinActive("ahk_exe msedge.exe")){
            ; Sleep(150)
            WinActivate("ahk_exe msedge.exe") ; Activates edge window
            ; Sleep(250)
            WinWaitActive("ahk_exe msedge.exe")
        }    
        ; Sleep(100)
    }
    ; sleep(250)
    WinMaximize("ahk_exe msedge.exe") ; Maximizes the Edge Window
    ; Sleep(80)
	SendInput("^t")
    ; Sleep(100)
	SendInput("website.com{Enter}") ; Accesses Login Site
    ; Sleep(250)
	b2baa1ImgFound1:=False ; Defines a false variable, which will turn true when site is fully loaded
    while(b2baa1ImgFound1=False){
        x1 := !ImageSearch(&b2baa_X, &b2baa_Y, 961, 407, 1086, 552, b2baa) ; If site is loaded, proceed
        if(x1=0){    
            b2baa1ImgFound1:=True
        }
        ; Sleep(100)
    }
	SendInput(MouseClick("Left", (2*b2baa_X+212)//2, (2*b2baa_Y+100)//2))
    ; Sleep(50)
	SendInput("{Tab}" b2ba.username "{Tab}" b2ba.password "{Enter}") ; Enters the username and password
    ; Sleep(50)
	b2baa1ImgFound2:=False ; Defines a false variable, which will turn true when site is fully loaded
    while(b2baa1ImgFound2=False){
        if(ImageSearch(&b2bab_X, &b2bab_Y, 110,146, 170,210, b2bab)){ ; If site is loaded, proceed
            b2baa1ImgFound2:=True
        }
        ; Sleep(100)
    }
	MouseMove((2*b2bab_X+34)//2, (2*b2bab_Y+34)//2)
    ToolTip("Website Fully Loaded", 604, 290)
    pspeaker.speak("Website Fully Loaded")
    SetTimer(ToolTip,-250)
}
User avatar
boiler
Posts: 17295
Joined: 21 Dec 2014, 02:44

Re: modify setbatchlines

25 Mar 2024, 06:07

You shouldn’t need sleeps for anything before maximizing the Edge window, but I would add them between most lines after that to give the browser a chance to respond to the last action before taking the next.

By the way, this construct is creating and managing a variable that is redundant with the return value of ImageSearch:

Code: Select all

	b2baa1ImgFound2:=False ; Defines a false variable, which will turn true when site is fully loaded
    while(b2baa1ImgFound2=False){
        if(ImageSearch(&b2bab_X, &b2bab_Y, 110,146, 170,210, b2bab)){ ; If site is loaded, proceed
            b2baa1ImgFound2:=True
        }
        ; Sleep(100)
    }

All of that could just be this:

Code: Select all

	while !ImageSearch(&b2bab_X, &b2bab_Y, 110,146, 170,210, b2bab) ; If site is loaded, proceed
		Sleep 10
marypoppins_1
Posts: 127
Joined: 28 Oct 2020, 02:58

Re: modify setbatchlines

25 Mar 2024, 07:06

okay thank you @boiler . i'll try the image search tweak (it'll most probably work). regarding the sleeps. when the msedge process doesn't even exist, i guess it works fine no problem (without sleeps). when it exists and is active also works fine. when it exists and is inactive it kind of becomes 50/50. check the attached image. if i open / look through several programs and msedge becomes so "far back" there's a 80% chance that i either get a black screen / msedge is activated then somehow deactivated (script can't continue) / msedge activates but freezes so im forced to reload the script and msedge. if i toggle between 1 or 2 apps alongside msedge, i.e. msedge isn't so "far down" there's a 95% chance it works normally.
cE3SD6EQ5p11.png
cE3SD6EQ5p11.png (758.82 KiB) Viewed 129 times
edit1: i just tried again with 16+ open programs and the ram nearly maxed out. it worked without sleeps (both while msedge didn't exist and when it existed and was "far back"). i guess i'll keep it this way for now until i encounter something, altho this is how it should actually be. thank you boiler.
i'm not sure if this is due to the fact that i might've set the script priority to low some time ago when i was trying something. i put it back to normal now.
Last edited by marypoppins_1 on 25 Mar 2024, 07:22, edited 1 time in total.
User avatar
boiler
Posts: 17295
Joined: 21 Dec 2014, 02:44

Re: modify setbatchlines

25 Mar 2024, 07:15

Have you checked your CPU usage, regardless of whether an AHK script is running?
marypoppins_1
Posts: 127
Joined: 28 Oct 2020, 02:58

Re: modify setbatchlines

25 Mar 2024, 07:26

yes, i do check my cpu usage. highest i saw was around 15-18% i think. with all the ram-filling programs on and some light computer work it just hangs around 10-15%. spamming the windows key got me around 30%
Code_IgMfz6fFqA.png
Code_IgMfz6fFqA.png (155.33 KiB) Viewed 119 times

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot] and 19 guests