What commands start a new thread?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

What commands start a new thread?

Post by joefiesta » 18 Aug 2022, 10:48

The help THREADS topic, https://www.autohotkey.com/docs/misc/Threads.htm,
states:
The current thread is defined as the flow of execution invoked by the most recent event; examples include hotkeys, SetTimer subroutines, custom menu items, and GUI events. The current thread can be executing commands within its own subroutine or within other subroutines called by that subroutine
But, what commands start a new thread? I can find NO EXPLICIT list of these.
examples include hotkeys, SetTimer subroutines, custom menu items, and GUI events.
implies, by saying "examples include" as opposed to "examples are",
that other things may start new threads.
I've never needed to explicitly start a new thread until today. (RUNWAIT command must be kicked off in a NEW thread if one wants to capture it's OutputVarPID.
It's doc includes, at OutputVarPID:
RunWait also supports this parameter, though its OutputVarPID must be checked in another thread (otherwise, the PID will be invalid because the process will have terminated by the time the line following RunWait executes).
Having great difficulty figuring out how to realize a failing Runwait occurred (without, of course, having the warning dialog presented).

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

Re: What commands start a new thread?

Post by Rohwedder » 18 Aug 2022, 11:22

Hallo,
you never needed to explicitly start a new thread until today?
Then try:

Code: Select all

Loop
{
	ToolTip,% "Main-Thread " A_Index
	Sleep, 500
}
q::
Loop, 9
{
	SoundBeep, 4000, 20
	ToolTip,% A_ThisLabel "-Thread " A_Index
	Sleep, 500
}
Return
w::
Loop, 9
{
	SoundBeep, 2000, 20
	ToolTip,% A_ThisLabel "-Thread " A_Index
	Sleep, 500
}
Return
e::
Loop, 9
{
	SoundBeep, 4000, 20
	ToolTip,% A_ThisLabel "-Thread " A_Index
	Sleep, 500
}
Return
or:

Code: Select all

#MaxThreadsPerHotkey 9
Loop
{
	ToolTip,% "Main-Thread " A_Index
	Sleep, 500
}
1::
2::
3::
Loop, 9
{
	SoundBeep,% A_ThisLabel*1000 , 20
	ToolTip,% A_ThisLabel "-Thread " A_Index
	Sleep, 500
}
Return

joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: What commands start a new thread?

Post by joefiesta » 18 Aug 2022, 11:33

@rohwedder: that is not what I am looking for.

From thread A I need to start--not with a hotkey but programmatically--the second thread B. I will then want to return to thread A from where I was in Thread A.
That is, thread B must finish before thread A resumes.

Additionally, thread B will have a RUNWAIT command, e.g.

Runwait, something.exe,, UseErrorLevel, somethingPID

and this runwait must have the ability to not launch. For example, a more reasonable and complicated Runwait, like I'm using:

mzHpgm := "C:\Program Files\MZHistoryView\MozillaHistoryView.exe"
outfile := "C:\Users\Joey\AppData\Local\MZHistoryView\commaDefault.txt"
prof := "C:\Users\Joey\AppData\Roaming\Mozilla\Firefox\Profiles\hymutfwi.Joey-2"
runWait "%mzHpgm%" /scomma "%outfile%" -file "%prof%" , UseErrorLevel, MZPid


Yes, I could validate mzHpgm, outfile and prof. However, (1) I don't want to and (2) I may still not know whether the mzHpgm will launch.

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

Re: What commands start a new thread?

Post by Rohwedder » 18 Aug 2022, 11:44

Then:

Code: Select all

q::
Loop, 9
{
	SoundBeep, 4000, 20
	ToolTip,% A_ThisLabel "-Thread " A_Index
	IF A_Index = 5
		SetTimer, w, -100 ;starts programmatically the second thread w
	Sleep, 500
}
Return
w:
Loop, 9
{
	SoundBeep, 2000, 20
	ToolTip,% A_ThisLabel "-Thread " A_Index
	Sleep, 500
}
Return
Starting exes (Run or Runwait) is not called starting a new thread in this forum.

joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: What commands start a new thread?

Post by joefiesta » 18 Aug 2022, 12:45

@Rohwedder - that looks like it will do the trick. And, I could have figured that out.

But, what I'm really looking for is the answer to my question:

What commands start a new thread?

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

Re: What commands start a new thread?

Post by Rohwedder » 19 Aug 2022, 00:17

Counter question! Take this example from the manual:
https://www.autohotkey.com/docs/commands/Menu.htm#ExBasic

Code: Select all

#Persistent  ; Keep the script running until the user exits it.
Menu, Tray, Add  ; Creates a separator line.
Menu, Tray, Add, Item1, MenuHandler  ; Creates a new menu item.
return

MenuHandler:
MsgBox You selected %A_ThisMenuItem% from menu %A_ThisMenu%.
return
If you select with RButton Item1 of the tray menu, a new thread will be started. What command of this script then started that thread?

joefiesta
Posts: 497
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: What commands start a new thread?

Post by joefiesta » 20 Aug 2022, 08:36

Your question, Rohwebber, what command...started the new thread, in your example is easy to answer. The menu command started the new thread. Yes, not until the menu was used, however. But, documentation states, at MENU, that a new thread is created to run the given subroutine at the given label (assuming it is a label and not a function).

So, MENU creates a new thread.
What about GOSUB? that runs a subroutine. Does it create a new thread?

Answer: I have no idea because the documentation for GOSUB does not say so.

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

Re: What commands start a new thread?

Post by Rohwedder » 20 Aug 2022, 13:40

(Rohwedder not Rohwebber)
No, Goto and Gosub are both just continuations of the current thread.
Above list:
hotkeys, SetTimer subroutines, custom menu items, and GUI events
I can add: The Hotkey command, Hotstrings, InputHook(), OnMessage(), OnExit() ,
OnClipboardChange(), ComObjConnect() and the mouse hook, e.g.:

Code: Select all

#Persistent
DllCall("SetWindowsHookEx", "UInt", 14, "Ptr"
, RegisterCallback("LowLevelMouseProc"), "Ptr", 0, "UInt", 0, "Ptr")
Return
LowLevelMouseProc(nCode, wParam) {
    if wParam = 0x0200 ; WM_MOUSEMOVE
        ToolTip,% A_TickCount
}
I suppose when you see something that is a car, you know it is a car. Do you need a complete list of all car models for this skill?

iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: What commands start a new thread?

Post by iseahound » 20 Aug 2022, 23:33

Oh. You're probably looking for CreateThread, but AHK is not thread-safe - so you can't start a new thread. Every AutoHotkey thread is really just a bunch of routines interrupting each other, fast enough so that it appears multiple events are happening at the same time.
From thread A I need to start--not with a hotkey but programmatically--the second thread B. I will then want to return to thread A from where I was in Thread A.
That is, thread B must finish before thread A resumes.
Why do you need to start a new thread here? You are defining what a coroutine is. The following pseudo code example demonstrates coroutines - omitting the return statement so as to not reach the AHK limit of 500-2000 frames on the call stack.

Code: Select all

a() {
   SetTimer b, -100
}
b() {
   SetTimer a, -100
}
I will then want to return to thread A from where I was in Thread A.
This is just a function call statement.

For some examples look at Helgef's posts in the thread here: viewtopic.php?f=14&t=104815

A OnMessage example is provided that can satisfy your condition of starting a synchronous thread.

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: What commands start a new thread?

Post by lexikos » 21 Aug 2022, 17:17

Generally, commands do not start a new thread, nor do functions. A thread is started in response to an event that should call a callback function or subroutine.

To programmatically start a new thread, the most direct way is to use RegisterCallback and DllCall. The thread is started by the callback.

Post Reply

Return to “Ask for Help (v1)”