About AhkThread (Parent, Child and Sibling)

Ask for help, how to use AHK_H, etc.
User avatar
manehscripts
Posts: 102
Joined: 03 May 2019, 16:10

Re: About AhkThread (Parent, Child and Sibling)

25 Jul 2019, 10:36

HotKeyIt wrote:
25 Jul 2019, 05:52
Use the same way we used CriticalObject above (share) and do share.DllOtherThread....
If you can't get it right, post full example script.
Hi!
Okay, I have about 15 to 20 threads that work at the same time. Not to the same degree as in this example, but I forced it to show that it crashes.
I have some Threads that need to synchronize with each other, but several others work independently, as per the example below.

Code: Select all

#NoEnv
#NoTrayIcon
SetBatchlines, -1
#SingleInstance, Force
Global S1Count
Global S2Count
Global StopDllS1
Global StopDllS2
Global StopDllS3
Global StopDllS4
Global StopDllS5

share:=CriticalObject({S1Count:0,S2Count:0,DllThreadS1:"",DllThreadS2:"",DllThreadS3:"",DllThreadS4:"",DllThreadS5:""})
CalcObj := Func("Calc")

Gui, +AlwaysOnTop
Gui, Add, Button, h50 w250 vButtonS1 gS1, START S1
Gui, Add, Button, h50 w250 vButtonS2 gS2, START S2
Gui, Add, Button, h50 w250 vButtonS3 gS3, START S3
Gui, Add, Button, h50 w250 vButtonS4 gS4, START S4
Gui, Add, Button, h50 w250 vButtonS5 gS5, START S5
Gui, Show, `t
OnExit, ExitScript
return

S1:
    toggleS1 := !toggleS1
    if (toggleS1) {
		ahkthread_free(share.DllThreadS1),share.DllThreadS1:="",cS1:=""
		Sleep, 500
		scriptS1 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			PaternThread := AhkExported()
			share := CriticalObject("(&share)")
			CalcObj := Object("(&CalcObj)")
			Loop
			{
				if (StopDllS1 := PaternThread.AhkGetVar.StopDllS1) {
					SoundBeep, 8000
					share.S1Count := 0
					ExitApp
				}
				share.S1Count := A_Index
				S2CountIn := share.S2Count
				if (cS1 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S1 (say: Hello): `%cS1`% - S2 count is: `%S2CountIn`%, 0, 0
				}
			}
		)"
		GuiControl, disable, ButtonS1 ; disable to the user from not clicking the button too fast
		StopDllS1 := ""
		while !share.DllThreadS1.ahkReady() {
			share.DllThreadS1 := AhkThread(scriptS1)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS1,  STOP S1
		Sleep, 500
		GuiControl, enable, ButtonS1
    } else {
		StopDllS1 := 1
		GuiControl, disable, ButtonS1 ; disable to the user from not clicking the button too fast
		while share.DllThreadS1.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS1,  START S1
		Sleep, 500
		GuiControl, enable, ButtonS1
	}
return

S2:
    toggleS2 := !toggleS2
    if (toggleS2) {
		ahkthread_free(share.DllThreadS2),share.DllThreadS2:="",cS2:=""
		Sleep, 500
		scriptS2 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			PaternThread := AhkExported()
			share := CriticalObject("(&share)")
			CalcObj := Object("(&CalcObj)")
			Loop
			{
				if (StopDllS2 := PaternThread.AhkGetVar.StopDllS2) {
					SoundBeep, 8000
					share.S2Count := 0
					ExitApp
				}
				share.S2Count := A_Index
				S1CountIn := share.S1Count
				if (cS2 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S2 (say: Friend): `%cS2`% - S1 count is: `%S1CountIn`%, 0, 20
				}
			}
		)"
		GuiControl, disable, ButtonS2 ; disable to the user from not clicking the button too fast
		StopDllS2 := ""
		while !share.DllThreadS2.ahkReady() {
			share.DllThreadS2 := AhkThread(scriptS2)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS2,  STOP S2
		Sleep, 500
		GuiControl, enable, ButtonS2
    } else {
		StopDllS2 := 1
		GuiControl, disable, ButtonS2 ; disable to the user from not clicking the button too fast
		while share.DllThreadS2.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS2,  START S2
		Sleep, 500
		GuiControl, enable, ButtonS2
	}
return

S3:
    toggleS3 := !toggleS3
    if (toggleS3) {
		ahkthread_free(share.DllThreadS3),share.DllThreadS3:="",cS3:=""
		Sleep, 500
		scriptS3 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			PaternThread := AhkExported()
			CalcObj := Object("(&CalcObj)")
			Loop
			{
				if (StopDllS3 := PaternThread.AhkGetVar.StopDllS3) {
					SoundBeep, 8000
					ExitApp
				}
				if (cS3 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S3 (say: Dog): `%cS3`%, 0, 40
				}
			}
		)"
		GuiControl, disable, ButtonS3 ; disable to the user from not clicking the button too fast
		StopDllS3 := ""
		while !share.DllThreadS3.ahkReady() {
			share.DllThreadS3 := AhkThread(scriptS3)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS3,  STOP S3
		Sleep, 500
		GuiControl, enable, ButtonS3
    } else {
		StopDllS3 := 1
		GuiControl, disable, ButtonS3 ; disable to the user from not clicking the button too fast
		while share.DllThreadS3.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS3,  START S3
		Sleep, 500
		GuiControl, enable, ButtonS3
	}
return

S4:
    toggleS4 := !toggleS4
    if (toggleS4) {
		ahkthread_free(share.DllThreadS4),share.DllThreadS4:="",cS4:=""
		Sleep, 500
		scriptS4 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			PaternThread := AhkExported()
			CalcObj := Object("(&CalcObj)")
			Loop
			{
				if (StopDllS4 := PaternThread.AhkGetVar.StopDllS4) {
					SoundBeep, 8000
					ExitApp
				}
				if (cS4 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S4 (say: Cat): `%cS4`%, 0, 60
				}
			}
		)"
		GuiControl, disable, ButtonS4 ; disable to the user from not clicking the button too fast
		StopDllS4 := ""
		while !share.DllThreadS4.ahkReady() {
			share.DllThreadS4 := AhkThread(scriptS4)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS4,  STOP S4
		Sleep, 500
		GuiControl, enable, ButtonS4
    } else {
		StopDllS4 := 1
		GuiControl, disable, ButtonS4 ; disable to the user from not clicking the button too fast
		while share.DllThreadS4.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS4,  START S4
		Sleep, 500
		GuiControl, enable, ButtonS4
	}
return

S5:
    toggleS5 := !toggleS5
    if (toggleS5) {
		ahkthread_free(share.DllThreadS5),share.DllThreadS5:="",cS5:=""
		Sleep, 500
		scriptS5 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			PaternThread := AhkExported()
			CalcObj := Object("(&CalcObj)")
			Loop
			{
				if (StopDllS5 := PaternThread.AhkGetVar.StopDllS5) {
					SoundBeep, 8000
					ExitApp
				}
				if (cs5 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S5 (say: Brothers): `%cs5`%, 0, 80
				}
			}
		)"
		GuiControl, disable, ButtonS5 ; disable to the user from not clicking the button too fast
		StopDllS5 := ""
		while !share.DllThreadS5.ahkReady() {
			share.DllThreadS5 := AhkThread(scriptS5)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS5,  STOP S5
		Sleep, 500
		GuiControl, enable, ButtonS5
    } else {
		StopDllS5 := 1
		GuiControl, disable, ButtonS5 ; disable to the user from not clicking the button too fast
		while share.DllThreadS5.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS5,  START S5
		Sleep, 500
		GuiControl, enable, ButtonS5
	}
return

ExitScript:
	if share.DllThreadS1.ahkReady() {
        StopDllS1:=1
		while !share.DllThreadS1.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS2.ahkReady() {
        StopDllS2:=1
		while !share.DllThreadS2.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS3.ahkReady() {
        StopDllS3:=1
		while !share.DllThreadS3.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS4.ahkReady() {
        StopDllS4:=1
		while !share.DllThreadS4.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS5.ahkReady() {
        StopDllS5:=1
		while !share.DllThreadS5.ahkReady() {
			Sleep, 500
		}
	}
	Sleep, 500
	SoundBeep, 1000
	ExitApp
return

GuiClose:
ExitApp

Calc(x, y){
	return x + y
}
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 1893
Joined: 29 Sep 2013, 18:35
Contact:

Re: About AhkThread (Parent, Child and Sibling)

25 Jul 2019, 15:35

As mentioned use CriticalObject to communicate and share variables between dlls:

Code: Select all

#NoEnv
#NoTrayIcon
SetBatchlines, -1
#SingleInstance, Force
Global share, StopDll

share:=CriticalObject({S1Count:0,S2Count:0,DllThreadS1:"",DllThreadS2:"",DllThreadS3:"",DllThreadS4:"",DllThreadS5:""})
StopDll:=CriticalObject({S1:0,S2:0,S3:0,S4:0,S5:0})
CalcObj := CriticalObject(Func("Calc"))

Gui, +AlwaysOnTop
Gui, Add, Button, h50 w250 vButtonS1 gS1, START S1
Gui, Add, Button, h50 w250 vButtonS2 gS2, START S2
Gui, Add, Button, h50 w250 vButtonS3 gS3, START S3
Gui, Add, Button, h50 w250 vButtonS4 gS4, START S4
Gui, Add, Button, h50 w250 vButtonS5 gS5, START S5
Gui, Show, `t
OnExit, ExitScript
return

S1:
    toggleS1 := !toggleS1
    if (toggleS1) {
		ahkthread_free(share.DllThreadS1),share.DllThreadS1:="",cS1:=""
		Sleep, 500
		scriptS1 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			share := CriticalObject(" (&share) ")
			StopDll := CriticalObject(" (&StopDll) ")
			CalcObj := CriticalObject(" (&CalcObj) ")
			Loop
			{
				if (StopDll.S1) {
					SoundBeep, 8000
					share.S1Count := 0
					ExitApp
				}
				share.S1Count := A_Index
				S2CountIn := share.S2Count
				if (cS1 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S1 (say: Hello): `%cS1`% - S2 count is: `%S2CountIn`%, 0, 0
				}
			}
		)"
		GuiControl, disable, ButtonS1 ; disable to the user from not clicking the button too fast
		StopDll.S1 := 0
		while !share.DllThreadS1.ahkReady() {
			share.DllThreadS1 := AhkThread(scriptS1)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS1,  STOP S1
		Sleep, 500
		GuiControl, enable, ButtonS1
    } else {
		StopDll.S1 := 1
		GuiControl, disable, ButtonS1 ; disable to the user from not clicking the button too fast
		while share.DllThreadS1.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS1,  START S1
		Sleep, 500
		GuiControl, enable, ButtonS1
	}
return

S2:
    toggleS2 := !toggleS2
    if (toggleS2) {
		ahkthread_free(share.DllThreadS2),share.DllThreadS2:="",cS2:=""
		Sleep, 500
		scriptS2 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			share := CriticalObject(" (&share) ")
			StopDll := CriticalObject(" (&StopDll) ")
			CalcObj := CriticalObject(" (&CalcObj) ")
			Loop
			{
				if (StopDll.S2) {
					SoundBeep, 8000
					share.S2Count := 0
					ExitApp
				}
				share.S2Count := A_Index
				S1CountIn := share.S1Count
				if (cS2 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S2 (say: Friend): `%cS2`% - S1 count is: `%S1CountIn`%, 0, 20
				}
			}
		)"
		GuiControl, disable, ButtonS2 ; disable to the user from not clicking the button too fast
		StopDll.S2 := 0
		while !share.DllThreadS2.ahkReady() {
			share.DllThreadS2 := AhkThread(scriptS2)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS2,  STOP S2
		Sleep, 500
		GuiControl, enable, ButtonS2
    } else {
		StopDll.S2 := 1
		GuiControl, disable, ButtonS2 ; disable to the user from not clicking the button too fast
		while share.DllThreadS2.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS2,  START S2
		Sleep, 500
		GuiControl, enable, ButtonS2
	}
return

S3:
    toggleS3 := !toggleS3
    if (toggleS3) {
		ahkthread_free(share.DllThreadS3),share.DllThreadS3:="",cS3:=""
		Sleep, 500
		scriptS3 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			StopDll := CriticalObject(" (&StopDll) ")
			CalcObj := CriticalObject(" (&CalcObj) ")
			Loop
			{
				if (StopDll.S3) {
					SoundBeep, 8000
					ExitApp
				}
				if (cS3 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S3 (say: Dog): `%cS3`%, 0, 40
				}
			}
		)"
		GuiControl, disable, ButtonS3 ; disable to the user from not clicking the button too fast
		StopDll.S3 := 0
		while !share.DllThreadS3.ahkReady() {
			share.DllThreadS3 := AhkThread(scriptS3)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS3,  STOP S3
		Sleep, 500
		GuiControl, enable, ButtonS3
    } else {
		StopDll.S3 := 1
		GuiControl, disable, ButtonS3 ; disable to the user from not clicking the button too fast
		while share.DllThreadS3.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS3,  START S3
		Sleep, 500
		GuiControl, enable, ButtonS3
	}
return

S4:
    toggleS4 := !toggleS4
    if (toggleS4) {
		ahkthread_free(share.DllThreadS4),share.DllThreadS4:="",cS4:=""
		Sleep, 500
		scriptS4 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			StopDll := CriticalObject(" (&StopDll) ")
			CalcObj := CriticalObject(" (&CalcObj) ")
			Loop
			{
				if (StopDll.S4) {
					SoundBeep, 8000
					ExitApp
				}
				if (cS4 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S4 (say: Cat): `%cS4`%, 0, 60
				}
			}
		)"
		GuiControl, disable, ButtonS4 ; disable to the user from not clicking the button too fast
		StopDll.S4 := 0
		while !share.DllThreadS4.ahkReady() {
			share.DllThreadS4 := AhkThread(scriptS4)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS4,  STOP S4
		Sleep, 500
		GuiControl, enable, ButtonS4
    } else {
		StopDll.S4 := 1
		GuiControl, disable, ButtonS4 ; disable to the user from not clicking the button too fast
		while share.DllThreadS4.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS4,  START S4
		Sleep, 500
		GuiControl, enable, ButtonS4
	}
return

S5:
    toggleS5 := !toggleS5
    if (toggleS5) {
		ahkthread_free(share.DllThreadS5),share.DllThreadS5:="",cS5:=""
		Sleep, 500
		scriptS5 := "
		(
			#NoEnv
			#NoTrayIcon
			SetBatchlines, -1
			CoordMode, Tooltip
			CoordMode, Pixel, Screen
			CoordMode, Mouse

			StopDll := CriticalObject(" (&StopDll) ")
			CalcObj := CriticalObject(" (&CalcObj) ")
			Loop
			{
				if (StopDll.S5) {
					SoundBeep, 8000
					ExitApp
				}
				if (cs5 := CalcObj.(1, A_Index) ) {
					ToolTip, Thread S5 (say: Brothers): `%cs5`%, 0, 80
				}
			}
		)"
		GuiControl, disable, ButtonS5 ; disable to the user from not clicking the button too fast
		StopDll.S5 := 0
		while !share.DllThreadS5.ahkReady() {
			share.DllThreadS5 := AhkThread(scriptS5)
		}
		SoundBeep, 4000
		GuiControl,, ButtonS5,  STOP S5
		Sleep, 500
		GuiControl, enable, ButtonS5
    } else {
		StopDll.S5 := 1
		GuiControl, disable, ButtonS5 ; disable to the user from not clicking the button too fast
		while share.DllThreadS5.ahkReady() {
			Sleep, 500
		}
		GuiControl,, ButtonS5,  START S5
		Sleep, 500
		GuiControl, enable, ButtonS5
	}
return

ExitScript:
	if share.DllThreadS1.ahkReady() {
        StopDll.S1:=1
		while !share.DllThreadS1.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS2.ahkReady() {
        StopDll.S2:=1
		while !share.DllThreadS2.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS3.ahkReady() {
        StopDll.S3:=1
		while !share.DllThreadS3.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS4.ahkReady() {
        StopDll.S4:=1
		while !share.DllThreadS4.ahkReady() {
			Sleep, 500
		}
	}
	if share.DllThreadS5.ahkReady() {
        StopDll.S5:=1
		while !share.DllThreadS5.ahkReady() {
			Sleep, 500
		}
	}
	Sleep, 500
	SoundBeep, 1000
	ExitApp
return

GuiClose:
ExitApp

Calc(x, y){
	return x + y
}
User avatar
manehscripts
Posts: 102
Joined: 03 May 2019, 16:10

Re: About AhkThread (Parent, Child and Sibling)

26 Jul 2019, 05:35

@HotKeyIt thank's my friend!!! :dance: :bravo: :thumbup:
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!

Return to “Ask For Help”

Who is online

Users browsing this forum: pneumatic and 7 guests