How to manipulate an external Gui inside the Thread? Topic is solved

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

How to manipulate an external Gui inside the Thread?

10 Mar 2021, 00:05

Code: Select all

obj := CriticalObject({Dll:""})
Gui, Add, Text, x10 y10 h20 w100 vVar, First Text
Gui, Show, w250 h250, Test
gosub, start
return

start:
	toggle := !toggle
	if (toggle) {
		ahkthread_free(obj.Dll),obj.Dll:=""
		script := "
		(
			#NoEnv
			obj := CriticalObject(" (&obj) ")
			MsgBox, Thread OK
			GuiControl,, Var, TEXT FROM THREAD
		)"

		while !obj.Dll.ahkReady() {
			obj.Dll := AhkThread(script)
		}
	} else {
		while obj.Dll.ahkReady() {
			obj.Dll.ahkTerminate()
		}
	}
return

Esc::
	ExitApp
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to manipulate an external Gui inside the Thread?  Topic is solved

10 Mar 2021, 03:51

The same way you do it for external window:

Code: Select all

obj := CriticalObject({Dll:""})
Gui, Add, Text, x10 y10 h20 w200 vVar hwndhText, First Text
Gui, Show, w250 h250, Test
gosub, start
return

start:
	toggle := !toggle
	if (toggle) {
		ahkthread_free(obj.Dll),obj.Dll:=""
		script := "
		(
			#NoEnv
			obj := CriticalObject(" (&obj) ")
			MsgBox, Thread OK
			ControlSetText,, TEXT FROM THREAD, ahk_id " hText "
		)"

		while !obj.Dll.ahkReady() {
			obj.Dll := AhkThread(script)
		}
	} else {
		while obj.Dll.ahkReady() {
			obj.Dll.ahkTerminate()
		}
	}
return

Esc::
	ExitApp
Alternatively you can execute code in exe:

Code: Select all

obj := CriticalObject({Dll:""})
Gui, Add, Text, x10 y10 h20 w200 vVar hwndhText, First Text
Gui, Show, w250 h250, Test
gosub, start
return

start:
	toggle := !toggle
	if (toggle) {
		ahkthread_free(obj.Dll),obj.Dll:=""
		script := "
		(
			#NoEnv
			obj := CriticalObject(" (&obj) ")
			MsgBox, Thread OK
			exe:=AhkExported()
			exe.ahkExec(""GuiControl,, Var, TEXT FROM THREAD"")
		)"

		while !obj.Dll.ahkReady() {
			obj.Dll := AhkThread(script)
		}
	} else {
		while obj.Dll.ahkReady() {
			obj.Dll.ahkTerminate()
		}
	}
return

Esc::
	ExitApp
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 10:24

@HotKeyIt thank you, bro!

I'm trying to manipulate a listview but dont work. Please, could you tell me if I'm doing something wrong?

Code: Select all

EXE.ahkExec(""Gui, Submit, Nohide"")
EXE.ahkExec(""Gui, ListView, Var"")
LV_Delete()
EXE.ahkExec(""GuiControl, -Redraw, Var"")
Loop, Read, `%File`%
{
	Col1=
	Col2=
	StringSplit, Col, A_LoopReadLine, `%File`%,
	LV_Add("""", Col1, Col2)
}
LV_ModifyCol(1,""Integer AutoHdr SortDesc"")
LV_ModifyCol(1, 0)
LV_Modify(1, ""Vis Select"")
EXE.ahkExec(""GuiControl, +Redraw, Var"")
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 12:34

HotKeyIt wrote:
10 Mar 2021, 12:12
Can you post the full script

Code: Select all

#NoEnv
LOG := CriticalObject({Dll:""})

Gui, Add, ListView, x15 y15 w420 altsubmit +Grid r7 +hscroll vReadsLog, ID|Data
Gui, Add, Button, x15 y165 w420 h25 gListViewThread, New Data
Gui, Show, w450 h200, Test

OnExit, ExitProgram
return

ListViewThread:
	ahkthread_free(LOG.Dll),LOG.Dll:=""
	script := "
	(
		#NoEnv
		#NoTrayIcon
		ListLines Off
		#KeyHistory 0
		SendMode Input
		SetTitleMatchMode 2
		SetTitleMatchMode Fast
		CoordMode, Tooltip
		CoordMode, Pixel, Screen
		CoordMode, Mouse, Screen
		
		LOG := CriticalObject(" (&LOG) ")
		EXE := AhkExported()
		LogDir = `%A_ScriptDir`%\Log.txt
		Delimiter = ``;
		
		ID = 0
		Loop, Read, `%LogDir`%
			ID = `%A_Index`%
		ID += 1
		Data := `% A_YYYY
		FileAppend, `% ID . """" . Delimiter . """" . Data . ""``r``n"", `%LogDir`%
		MsgBox, Added to the Log.txt file!
		
		EXE.ahkExec(""Gui, Submit, Nohide"")
		EXE.ahkExec(""Gui, ListView, ReadsLog"")
		LV_Delete()
		EXE.ahkExec(""GuiControl, -Redraw, ReadsLog"")
		Loop, Read, `%LogDir`%
		{
			Col1=
			Col2=
			StringSplit, Col, A_LoopReadLine, `%Delimiter`%,
			LV_Add("""", Col1, Col2)
		}
		LV_ModifyCol(1,""Integer AutoHdr SortDesc"")
		LV_ModifyCol(1, 0)
		LV_Modify(1, ""Vis Select"")
		EXE.ahkExec(""GuiControl, +Redraw, ReadsLog"")
		
	)"
	while !LOG.Dll.ahkReady() {
		LOG.Dll := AhkThread(script)
	}
return

ExitProgram:
	if LOG.Dll.ahkReady() {
		while LOG.Dll.ahkReady() {
			LOG.Dll.ahkTerminate()
			Sleep, 500
		}
	}
	ExitApp
return

GuiClose:
ExitApp

Esc::
	ExitApp
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 16:57

Code: Select all

#NoEnv
LOG := CriticalObject({Dll:""})

Gui, Add, ListView, x15 y15 w420 altsubmit +Grid r7 +hscroll vReadsLog, ID|Data
Gui, Add, Button, x15 y165 w420 h25 gListViewThread, New Data
Gui, Show, w450 h200, Test

OnExit, ExitProgram
return

ListViewThread:
	ahkthread_free(LOG.Dll),LOG.Dll:=""
	script := "
	(
		#NoEnv
		#NoTrayIcon
		ListLines Off
		#KeyHistory 0
		SendMode Input
		SetTitleMatchMode 2
		SetTitleMatchMode Fast
		CoordMode, Tooltip
		CoordMode, Pixel, Screen
		CoordMode, Mouse, Screen
		
		LOG := CriticalObject(" (&LOG) ")
		EXE := AhkExported()
		LogDir = `%A_ScriptDir`%\Log.txt
		Delimiter = ``;
		
		ID = 0
		Loop, Read, `%LogDir`%
			ID = `%A_Index`%
		ID += 1
		Data := `% A_YYYY
		FileAppend, `% ID . """" . Delimiter . """" . Data . ""``r``n"", `%LogDir`%
		MsgBox, Added to the Log.txt file!
		
		EXE.ahkExec(""Gui, Submit, Nohide``nGui, ListView, ReadsLog``nLV_Delete()``nGuiControl, -Redraw, ReadsLog"")
		Loop, Read, `%LogDir`%
		{
			Col1=
			Col2=
			StringSplit, Col, A_LoopReadLine, `%Delimiter`%,
			script.=""LV_Add("""""""","" Col1 "","" Col2 "")``n""
			Sleep 10
		}
		EXE.ahkExec(script ""LV_ModifyCol(1,""""Integer AutoHdr SortDesc"""")``nLV_ModifyCol(1, 0)``nLV_Modify(1, """"Vis Select"""")``nGuiControl, +Redraw, ReadsLog"")
		
	)"
	while !LOG.Dll.ahkReady() {
		LOG.Dll := AhkThread(script)
	}
return

ExitProgram:
	if LOG.Dll.ahkReady() {
		while LOG.Dll.ahkReady() {
			LOG.Dll.ahkTerminate()
			Sleep, 500
		}
	}
	ExitApp
return

GuiClose:
ExitApp

Esc::
	ExitApp
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 19:05

I'm testing this example with full time but the score is causing a problem ...
==> Missing ")" before ":"

Code: Select all

#NoEnv
LOG := CriticalObject({Dll:""})

Gui, Add, ListView, x15 y15 w420 altsubmit +Grid r7 +hscroll vReadsLog, ID|Data
Gui, Add, Button, x15 y165 w420 h25 gListViewThread, New Data
Gui, Show, w450 h200, Test

OnExit, ExitProgram
return

ListViewThread:
	ahkthread_free(LOG.Dll),LOG.Dll:=""
	script := "
	(
		#NoEnv
		#NoTrayIcon
		ListLines Off
		#KeyHistory 0
		SendMode Input
		SetTitleMatchMode 2
		SetTitleMatchMode Fast
		CoordMode, Tooltip
		CoordMode, Pixel, Screen
		CoordMode, Mouse, Screen
		
		LOG := CriticalObject(" (&LOG) ")
		EXE := AhkExported()
		LogDir = `%A_ScriptDir`%\Log.txt
		Delimiter = ``;
		
		ID = 0
		Loop, Read, `%LogDir`%
			ID = `%A_Index`%
		ID += 1
		Data := `% A_Hour . "":"" . A_Min . "":"" .  A_Sec . ""h - "" . A_DD . ""/"" . A_MM . ""/"" . A_YYYY
		FileAppend, `% ID . """" . Delimiter . """" . Data . ""``r``n"", `%LogDir`%
		MsgBox, Added to the Log.txt file!
		
		EXE.ahkExec(""Gui, Submit, Nohide``nGui, ListView, ReadsLog``nLV_Delete()``nGuiControl, -Redraw, ReadsLog"")
		Loop, Read, `%LogDir`%
		{
			Col1=
			Col2=
			StringSplit, Col, A_LoopReadLine, `%Delimiter`%,
			script.=""LV_Add("""""""","" Col1 "","" Col2 "")``n""
			Sleep 10
		}
		EXE.ahkExec(script ""LV_ModifyCol(1,""""Integer AutoHdr SortDesc"""")``nLV_ModifyCol(1, 0)``nLV_Modify(1, """"Vis Select"""")``nGuiControl, +Redraw, ReadsLog"")
		
	)"
	while !LOG.Dll.ahkReady() {
		LOG.Dll := AhkThread(script)
	}
return

ExitProgram:
	if LOG.Dll.ahkReady() {
		while LOG.Dll.ahkReady() {
			LOG.Dll.ahkTerminate()
			Sleep, 500
		}
	}
	ExitApp
return

GuiClose:
ExitApp

Esc::
	ExitApp
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 20:24

Code: Select all

#NoEnv
LOG := CriticalObject({Dll:""})

Gui, Add, ListView, x15 y15 w420 altsubmit +Grid r7 +hscroll vReadsLog, ID|Data
Gui, Add, Button, x15 y165 w420 h25 gListViewThread, New Data
Gui, Show, w450 h200, Test

OnExit, ExitProgram
return

ListViewThread:
	ahkthread_free(LOG.Dll),LOG.Dll:=""
	script := "
	(
		#NoEnv
		#NoTrayIcon
		ListLines Off
		#KeyHistory 0
		SendMode Input
		SetTitleMatchMode 2
		SetTitleMatchMode Fast
		CoordMode, Tooltip
		CoordMode, Pixel, Screen
		CoordMode, Mouse, Screen
		
		LOG := CriticalObject(" (&LOG) ")
		EXE := AhkExported()
		LogDir = `%A_ScriptDir`%\Log.txt
		Delimiter = ``;
		
		ID = 0
		Loop, Read, `%LogDir`%
			ID = `%A_Index`%
		ID += 1
		Data := `% A_Hour . "":"" . A_Min . "":"" .  A_Sec . ""h - "" . A_DD . ""/"" . A_MM . ""/"" . A_YYYY
		FileAppend, `% ID . "" . Delimiter . "" . Data . ""``r``n"", `%LogDir`%
		MsgBox, Added to the Log.txt file!
		
		EXE.ahkExec(""Gui, Submit, Nohide``nGui, ListView, ReadsLog``nLV_Delete()``nGuiControl, -Redraw, ReadsLog"")
		Loop, Read, `%LogDir`%
		{
			Col1=
			Col2=
			StringSplit, Col, A_LoopReadLine, `%Delimiter`%,
			script.=""LV_Add("""""""","""""" Col1 """""","""""" Col2 """""")``n""
			Sleep 10
		}
		EXE.ahkExec(script ""LV_ModifyCol(1,""""Integer AutoHdr SortDesc"""")``n`;LV_ModifyCol(1, 0)``nLV_Modify(1, """"Vis Select"""")``nGuiControl, +Redraw, ReadsLog"")
		
	)"
	while !LOG.Dll.ahkReady() {
		LOG.Dll := AhkThread(script)
	}
return

ExitProgram:
	if LOG.Dll.ahkReady() {
		while LOG.Dll.ahkReady() {
			LOG.Dll.ahkTerminate()
			Sleep, 500
		}
	}
	ExitApp
return

GuiClose:
ExitApp

Esc::
	ExitApp
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 20:34

HotKeyIt wrote:
10 Mar 2021, 20:24
Thank you so much your help, friend.
But could you tell me if there is any documentation that explains this huge amount of "quotes"? I would never be able to solve this without your help. Taking advantage of this text, is it safe to migrate to ahk_v2? He was always in Alpha, it’s one of the reasons I haven’t migrated yet. Thank you again!
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 22:08

AFAIK it is save to switch to v2 but there might be still syntax changes so might need to rewrite a lot of code ;)
It helps to show the script or copy past into editor:

Code: Select all

MsgBox % "
	(
		#NoEnv
		#NoTrayIcon
		ListLines Off
		#KeyHistory 0
		SendMode Input
		SetTitleMatchMode 2
		SetTitleMatchMode Fast
		CoordMode, Tooltip
		CoordMode, Pixel, Screen
		CoordMode, Mouse, Screen
		
		LOG := CriticalObject(" (&LOG) ")
		EXE := AhkExported()
		LogDir = `%A_ScriptDir`%\Log.txt
		Delimiter = ``;
		
		ID = 0
		Loop, Read, `%LogDir`%
			ID = `%A_Index`%
		ID += 1
		Data := `% A_Hour . "":"" . A_Min . "":"" .  A_Sec . ""h - "" . A_DD . ""/"" . A_MM . ""/"" . A_YYYY
		FileAppend, `% ID . "" . Delimiter . "" . Data . ""``r``n"", `%LogDir`%
		MsgBox, Added to the Log.txt file!
		
		EXE.ahkExec(""Gui, Submit, Nohide``nGui, ListView, ReadsLog``nLV_Delete()``nGuiControl, -Redraw, ReadsLog"")
		Loop, Read, `%LogDir`%
		{
			Col1=
			Col2=
			StringSplit, Col, A_LoopReadLine, `%Delimiter`%,
			script.=""LV_Add("""","""""" Col1 """""","""""" Col2 """""")``n""
			Sleep 10
		}
		EXE.ahkExec(script ""LV_ModifyCol(1,""""Integer AutoHdr SortDesc"""")``n`;LV_ModifyCol(1, 0)``nLV_Modify(1, """"Vis Select"""")``nGuiControl, +Redraw, ReadsLog"")
		
	)"
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 22:26

the documentation that explains this huge amount of quotes, is the documentation of the rules for escaping quotes(and other characters) in strings: https://www.autohotkey.com/docs/Language.htm#strings
if u know the rule, u can easily justify why there are so many
here, a script is written in the main script which is then passed to AhkThread(). AhkThread() requires strings, so the script requires 1-level of escaping
the thread script itself contains more code to be passed to AhkExec() which also requires strings, so now for this exec-script to work u need 2-levels of escaping(from the PoV of the main script)

v2 would have cut down on the need for escaping that many quotes since u would be able to alternate ' and ", however, this script is a far cry from being v2 compatible. the Gui system, for one, has been completely revamped
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How to manipulate an external Gui inside the Thread?

10 Mar 2021, 23:21

Correct, the need to escape " goes into multiple levels which makes the need of that many ".
User avatar
manehscripts
Posts: 126
Joined: 03 May 2019, 16:10

Re: How to manipulate an external Gui inside the Thread?

11 Mar 2021, 02:16

@swagfag and @HotKeyIt , thanks for your help!!
I understand that v2 is quite different. I need to get started with him someday.
-----------------------------------------------------------
Stop to think, shut up to resist, and act to win!

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 7 guests