Hello,
There's a few of your that's been helping/asking questions about the bot I've been building and well it works! Big thanks all round to those that've helped, I don't want to bother them with an @ but they know who they are.
To get around the fact AHK doesn't support multi threading I've ended up running 10 different scripts at once to make this bot work. It works pretty perfectly and I've a bat to kill the processes if necessary. It just ends all AHK processes.
What I'm trying to figure out is how to pause or toggle all the scripts at once. A quality of life change more than anything else.
Any thoughts?
Thanks
How do I pause multiple running scripts with one hotkey?
Re: How do I pause multiple running scripts with one hotkey?
You can have one script with the hotkey in it and when triggered, it sets a signal that the rest will be looking for. It would change the signal value back when it’s pressed again to unpause. This could be a value in a .ini file or the presence of a window (hidden or otherwise) or a number of things. The one script could send a message to the others using PostMessage and the others would use OnMessage to react to that event. See example #3 in the PostMessage documentation.
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How do I pause multiple running scripts with one hotkey?
You can launch the menu item "Pause" of the running script in this way:
Code: Select all
global WM_COMMAND := 0x111, ID_FILE_PAUSE := 65403, ID_FILE_TERMINATE := 65405
DetectHiddenWindows, On
ChildPIDs := []
OnExit( Func("TerminateChilds").Bind(ChildPIDs) )
Loop 10 {
script := GetScript(A_Index, 90 + A_Index * 30)
PID := ExecScript(script) ; ExecScript returns the Process ID of the new running script
ChildPIDs.Push(PID)
}
Return
^p:: ; pause/unpause all child scripts
for k, PID in ChildPIDs
PostMessage, WM_COMMAND, ID_FILE_PAUSE,,, ahk_pid %PID% ahk_class AutoHotkey
Return
Esc::ExitApp ; terminate all child scripts and exit
GetScript(param1, param2) {
script =
(
CoordMode, ToolTip
Loop {
ToolTip, Script %param1%: `%A_Index`%, 100, %param2%
Sleep, 300
}
)
Return script
}
ExecScript(script, exePath := "") {
(!exePath && exePath := A_AhkPath)
shell := ComObjCreate("WScript.Shell")
exec := shell.Exec(exePath . " *")
exec.StdIn.Write(script)
exec.StdIn.Close()
return exec.ProcessID
}
TerminateChilds(PIDs) {
for k, PID in PIDs
PostMessage, WM_COMMAND, ID_FILE_TERMINATE,,, ahk_pid %PID% ahk_class AutoHotkey
}
Re: How do I pause multiple running scripts with one hotkey?
@boiler
I'm using this currently to start and initialise the scripts. Do you mean adding a pause function to each and then running something like this?
This is working but it's a little clunky and I'm running out of easy to use hotkeys lol.
@teadrinker hello! I'm not sure what the menu item is, is this a ahk feature? when I try running that script I get a call to non-existent function on line 8.
I'm using this currently to start and initialise the scripts. Do you mean adding a pause function to each and then running something like this?
Code: Select all
Sleep 1000
Send {F11}
Sleep 1000
Send {F10}
Sleep 1000
Send {F9}
Sleep 1000
Send {F8}
Sleep 1000
Send {F7}
Sleep 1000
Send {F6}
Sleep 1000
Send {F5}
Sleep 1000
Send {F4}
Sleep 1000
Send {F3}
Sleep 1000
Send {F2}
Sleep 1000
Send {F1}
Code: Select all
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Attack().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Exit_PVP().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Galaxy().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Ammo_Health().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Keep_Looking().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Okay().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_PvP.ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_Yes().ahk"
Run, "C:\Users\x\OneDrive\AUTOHOTKEY\lib\LIB_Click_No_Acquire().ahk"
@teadrinker hello! I'm not sure what the menu item is, is this a ahk feature? when I try running that script I get a call to non-existent function on line 8.
Re: How do I pause multiple running scripts with one hotkey?
You would have to add something to one of them to write something to an ini file or do something else that each other script would monitor. Or follow the PostMessage example I mentioned.
Re: How do I pause multiple running scripts with one hotkey?
Thanks @boiler I'll do some reading on both, never worked with INI files. I've had a quick read, do these allow me to "set" process IDs or some other variable I can then work with?
Re: How do I pause multiple running scripts with one hotkey?
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How do I pause multiple running scripts with one hotkey?
I wasn’t saying that what I suggested was preferred over your approach. I wasn’t going to reply anymore after seeing your response, but then he asked a question about what I posted, so I was just responding to that. I’m sure it’s easier/better to follow what you posted, although I haven’t looked into it in detail.
-
- Posts: 4326
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How do I pause multiple running scripts with one hotkey?
Hallo,
start this script several times and all these scripts can be paused or terminated synchronously:But the synchronicity must not be destroyed by using the AutoHotkey icon.
start this script several times and all these scripts can be paused or terminated synchronously:
Code: Select all
#SingleInstance Off
~*Pause::Pause
~*Esc::ExitApp
Re: How do I pause multiple running scripts with one hotkey?
@teadrinker I did not copy it correctly sorry!
It's definitely doing something, I've no real idea what's happening. It's started a bunch of functions called * and I can start and stop them with ctrl+p
I'm assuming I need to set ChildPIDs somehow? Sorry very new with this and every one of these "asking for help" posts I post is a new rabbit hole!
@Rohwedder I can't get that to work unless I use different hotkeys for I thought?
@boiler this afternoons Ini reading has been useful in general many thanks! I assume I can use my script to set a bunch of variables like a database table and then call them with additional functions etc?
It's definitely doing something, I've no real idea what's happening. It's started a bunch of functions called * and I can start and stop them with ctrl+p
I'm assuming I need to set ChildPIDs somehow? Sorry very new with this and every one of these "asking for help" posts I post is a new rabbit hole!
@Rohwedder I can't get that to work unless I use different hotkeys for I thought?
@boiler this afternoons Ini reading has been useful in general many thanks! I assume I can use my script to set a bunch of variables like a database table and then call them with additional functions etc?
Re: How do I pause multiple running scripts with one hotkey?
Sure, that's one way to look at it. You can read and change values of different elements in the ini file without disturbing or having to parse through the rest of the file as you would have to with a regular text file. There are other types of files that can manage more complex structures of data if needed, such as xml or json, but ini is fine for a lot of cases, and it's easier to use.
Yes, you can use your script to set a bunch of variables' values in the ini file and they can be read in at any time to see what the latest setting is.
Re: How do I pause multiple running scripts with one hotkey?
Just about to sleep so I'll read some more tomorrow but could it say be used to set process IDs or variables I can then look for to end?
Re: How do I pause multiple running scripts with one hotkey?
Sure, there’s no reason why it couldn’t be used to store process IDs.