How do I pause multiple running scripts with one hotkey?

Get help with using AutoHotkey and its commands and hotkeys
saph
Posts: 55
Joined: 25 May 2020, 12:09

How do I pause multiple running scripts with one hotkey?

11 Jul 2020, 11:26

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
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How do I pause multiple running scripts with one hotkey?

11 Jul 2020, 12:42

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.
teadrinker
Posts: 1799
Joined: 29 Mar 2015, 09:41
Contact:

Re: How do I pause multiple running scripts with one hotkey?

11 Jul 2020, 12:51

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
}
saph
Posts: 55
Joined: 25 May 2020, 12:09

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 09:09

@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?

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"
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.
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 09:44

saph wrote:
13 Jul 2020, 09:09
@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?
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.
saph
Posts: 55
Joined: 25 May 2020, 12:09

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 11:19

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?
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 12:44

saph wrote:
13 Jul 2020, 11:19
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?
You can write pretty much anything you want to using IniWrite and the other scripts would read it using IniRead.
teadrinker
Posts: 1799
Joined: 29 Mar 2015, 09:41
Contact:

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 14:51

saph wrote: when I try running that script I get a call to non-existent function on line 8.
Are you sure you completely copied my code? The GetScript() function is in 21'st line.
teadrinker
Posts: 1799
Joined: 29 Mar 2015, 09:41
Contact:

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 14:55

boiler wrote: 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.
But why? Did you see my example?
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 15:08

teadrinker wrote:
13 Jul 2020, 14:55
But why? Did you see my example?
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.
Rohwedder
Posts: 3383
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 15:52

Hallo,
start this script several times and all these scripts can be paused or terminated synchronously:

Code: Select all

#SingleInstance Off
~*Pause::Pause
~*Esc::ExitApp
But the synchronicity must not be destroyed by using the AutoHotkey icon.
saph
Posts: 55
Joined: 25 May 2020, 12:09

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 17:49

@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?
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 18:48

saph wrote: @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?
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.
saph
Posts: 55
Joined: 25 May 2020, 12:09

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 19:46

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?
User avatar
boiler
Posts: 5909
Joined: 21 Dec 2014, 02:44

Re: How do I pause multiple running scripts with one hotkey?

13 Jul 2020, 20:51

Sure, there’s no reason why it couldn’t be used to store process IDs.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], braunbaer, brondog, Google [Bot], jamez, mikeyww and 48 guests