massive lag after 30mins of running my script Topic is solved

Ask gaming related questions (AHK v1.1 and older)
kentpachi
Posts: 152
Joined: 15 May 2016, 01:23

massive lag after 30mins of running my script

Post by kentpachi » 28 Dec 2023, 02:27

after 30mins, the game itself and the desktop is lagging after my script is running more than 30mins.


is their a way to fix this lag?

this is my script

Code: Select all

#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.
#singleinstance, force
#MaxThreadsPerHotkey 2
loop
{
;play
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 1695, 625, 1876, 695, *25 C:\Users\Zenken\Desktop\bot\play.png
If ErrorLevel = 0
	{
	Click, %FoundX%, %FoundY% Left, 1
sleep, 3000

}

sleep, 150

;sarn1

CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 512, 87, 546, 109, *25 C:\Users\Zenken\Desktop\bot\sarn11.png
If ErrorLevel = 0
{
	Click, 1345, 1038 Left, 1
sleep, 2300

}

;sarn2

CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 788, 670, 990, 887, *25 C:\Users\Zenken\Desktop\bot\sarn22.png
If ErrorLevel = 0
	{
	Click, %FoundX%, %FoundY% Left, 1
sleep, 1500

}


;sarn3
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 518, 243, 599, 318, *25 C:\Users\Zenken\Desktop\bot\sarn3.png
If ErrorLevel = 0
		{
		Send {LCtrl down}
	Click, %FoundX%, %FoundY% Left, 1
	Send {LCtrl up}
sleep, 1500

}


;sarn4
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 412, 287, 548, 381, *25 C:\Users\Zenken\Desktop\bot\sarn4.png
If ErrorLevel = 0
		{
	Click, %FoundX%, %FoundY% Left, 1
sleep, 6000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
send, {2}
send, {3}
send, {4}
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
send, {2}
send, {3}
send, {4}
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300
Click, 570, 125 Left, 1
sleep, 1000
send, {e}
sleep, 300


BlockInput On
Send {Enter}
Sleep 2
Send `/exit
Send {Enter}
Sleep, 10
BlockInput Off

sleep, 690
}

}
[Mod edit: Replaced c-tags (for inline code) with [code][/code] tags.]


after 30mins this script is running, the game and desltop are lagging and when i close the script, its back to normal which means the cause of lag is this script.

Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: massive lag after 30mins of running my script  Topic is solved

Post by Rohwedder » 28 Dec 2023, 04:30

Hallo,
I do not believe that the script is the cause of lagging. Test this with display of the ScriptProcessTimes (and possibly reload):

Code: Select all

#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.
#singleinstance, force
; #MaxThreadsPerHotkey 2
PID := DllCall("GetCurrentProcessId"), PrTi := 0
SetTimer, CPUTimes, 500 ; 500ms
loop
{
	;play
	CoordMode, Pixel, Window
	ImageSearch, FoundX, FoundY, 1695, 625, 1876, 695, *25 C:\Users\Zenken\Desktop\bot\play.png
	If ErrorLevel = 0
	{
		Click, %FoundX%, %FoundY% Left, 1
		sleep, 3000
	}
	sleep, 150
	;sarn1
	CoordMode, Pixel, Window
	ImageSearch, FoundX, FoundY, 512, 87, 546, 109, *25 C:\Users\Zenken\Desktop\bot\sarn11.png
	If ErrorLevel = 0
	{
		Click, 1345, 1038 Left, 1
		sleep, 2300
	}
	;sarn2
	CoordMode, Pixel, Window
	ImageSearch, FoundX, FoundY, 788, 670, 990, 887, *25 C:\Users\Zenken\Desktop\bot\sarn22.png
	If ErrorLevel = 0
	{
		Click, %FoundX%, %FoundY% Left, 1
		sleep, 1500
	}
	;sarn3
	CoordMode, Pixel, Window
	ImageSearch, FoundX, FoundY, 518, 243, 599, 318, *25 C:\Users\Zenken\Desktop\bot\sarn3.png
	If ErrorLevel = 0
	{
		Send {LCtrl down}
		Click, %FoundX%, %FoundY% Left, 1
		Send {LCtrl up}
		sleep, 1500
	}
	;sarn4
	CoordMode, Pixel, Window
	ImageSearch, FoundX, FoundY, 412, 287, 548, 381, *25 C:\Users\Zenken\Desktop\bot\sarn4.png
	If ErrorLevel = 0
	{
		Click, %FoundX%, %FoundY% Left, 1
		sleep, 6000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		send, {2}
		send, {3}
		send, {4}
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		send, {2}
		send, {3}
		send, {4}
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		Click, 570, 125 Left, 1
		sleep, 1000
		send, {e}
		sleep, 300
		BlockInput On
		Send {Enter}
		Sleep 2
		Send `/exit
		Send {Enter}
		Sleep, 10
		BlockInput Off
		sleep, 690
	}
}
CPUTimes:
PrTi := Round((PrTi*9 + GetProcessTimes(pid))/10,2)
ToolTip, ScriptProcessTime: %PrTi%
; IF PrTi > 5  ; possibly reload
	; Reload
Return
getProcessTimes(PID) {
 ; RHCP https://autohotkey.com/board/topic/113942-solved-get-cpu-usage-in/
 ; Return values
 ; -1 on first run 
 ; -2 if process doesn't exist or you don't have access to it
 ; Process cpu usage as percent of total CPU
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=73498
    static aPIDs := [], hasSetDebug
    ; If called too frequently, will get mostly 0%, so it's better to just return the previous usage 
    if aPIDs.HasKey(PID) && A_TickCount - aPIDs[PID, "tickPrior"] < 250
        return aPIDs[PID, "usagePrior"] 
   	; Open a handle with PROCESS_QUERY_LIMITED_INFORMATION access
    if !hProc := DllCall("OpenProcess", "UInt", 0x1000, "Int", 0, "Ptr", pid, "Ptr")
        return -2, aPIDs.HasKey(PID) ? aPIDs.Remove(PID, "") : "" ; Process doesn't exist anymore or don't have access to it.
         
    DllCall("GetProcessTimes", "Ptr", hProc, "Int64*", lpCreationTime, "Int64*", lpExitTime, "Int64*", lpKernelTimeProcess, "Int64*", lpUserTimeProcess)
    DllCall("CloseHandle", "Ptr", hProc)
    DllCall("GetSystemTimes", "Int64*", lpIdleTimeSystem, "Int64*", lpKernelTimeSystem, "Int64*", lpUserTimeSystem)
   
    if aPIDs.HasKey(PID) ; check if previously run
    {
        ; find the total system run time delta between the two calls
        systemKernelDelta := lpKernelTimeSystem - aPIDs[PID, "lpKernelTimeSystem"] ;lpKernelTimeSystemOld
        systemUserDelta := lpUserTimeSystem - aPIDs[PID, "lpUserTimeSystem"] ; lpUserTimeSystemOld
        ; get the total process run time delta between the two calls 
        procKernalDelta := lpKernelTimeProcess - aPIDs[PID, "lpKernelTimeProcess"] ; lpKernelTimeProcessOld
        procUserDelta := lpUserTimeProcess - aPIDs[PID, "lpUserTimeProcess"] ;lpUserTimeProcessOld
        ; sum the kernal + user time
        totalSystem :=  systemKernelDelta + systemUserDelta
        totalProcess := procKernalDelta + procUserDelta
        ; The result is simply the process delta run time as a percent of system delta run time
        result := 100 * totalProcess / totalSystem
    }
    else result := -1

    aPIDs[PID, "lpKernelTimeSystem"] := lpKernelTimeSystem
    aPIDs[PID, "lpKernelTimeSystem"] := lpKernelTimeSystem
    aPIDs[PID, "lpUserTimeSystem"] := lpUserTimeSystem
    aPIDs[PID, "lpKernelTimeProcess"] := lpKernelTimeProcess
    aPIDs[PID, "lpUserTimeProcess"] := lpUserTimeProcess
    aPIDs[PID, "tickPrior"] := A_TickCount
    return aPIDs[PID, "usagePrior"] := result 
}

User avatar
boiler
Posts: 17395
Joined: 21 Dec 2014, 02:44

Re: massive lag after 30mins of running my script

Post by boiler » 28 Dec 2023, 21:51

@kentpachi -- As has been pointed out before and you say that you noted here, please do not use the c tags (meant for inline code) instead of code tags, which produce the code boxes. The moderators have continued to modify your posts to replace the tags and noted it in your posts each time.

Post Reply

Return to “Gaming Help (v1)”