thqby's AutoHotkey_H v2.0 & v2.1

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

thqby's AutoHotkey_H v2.0 & v2.1

29 Jan 2024, 07:11

thqby's AutoHotkey_H started as a fork of AutoHotkey_L v2, merging the branch HotKeyIt/ahkdll-v2, but with some changes.
See the README of each branch for details.
Attachments
Ahk2Exe.zip
(468.07 KiB) Downloaded 230 times
Last edited by thqby on 28 Mar 2024, 21:31, edited 1 time in total.
m3user
Posts: 243
Joined: 17 Jan 2014, 18:11

Re: thqby's AutoHotkey_H v2.0 & v2.1

29 Jan 2024, 10:50

Any chance of merging v1 as well? Thanks.
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

30 Jan 2024, 10:33

No interest in merging v1, debugging v1 scripts is maddening.
changxiaotiao
Posts: 6
Joined: 12 May 2024, 19:52
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

12 May 2024, 20:05

I am an AutoHotkey beginner and wanted to use multi-threading, so I installed the exe and dll files you provided. I can use the NewThread function, but I get an error when using other functions like ahkPause, with the message "Error: This value of type 'Integer' has no method named 'ahkPause'." I have tried versions 2.0.12 and 2.0.14, both of which gave me errors, but it runs fine with the official download of v2.0-beta.1. Did I miss any installation steps? Below is the code I found in the official documentation and tried to run.

Code: Select all

#Requires AutoHotkey v2.0.12
#SingleInstance Force
dllpath:=A_AhkDir "\AutoHotkey.dll"

dll:=NewThread("
(
Persistent
Loop
	ToolTip A_TickCount
)",,dllpath)
Sleep 1000
dll.ahkPause("On")

[Mod edit: Fixed the codebox tags. They go around the code, not both tags before the code.]
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

13 May 2024, 08:22

Maybe you used to use ahk-wrapped function NewThread. I didn't package these into ahk. You can still use these features through functions like ahkPause(1, NewThread(...)).

You can see the changes in the readme of the github repository.
changxiaotiao
Posts: 6
Joined: 12 May 2024, 19:52
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

13 May 2024, 21:42

Thank you very much for your guidance. I've learned how to use ahkPause, but I have encountered another issue regarding terminating a thread.
I found that Thread("Terminate", false, dll) does not work, while ahkPause does work. I would like to know how to properly terminate a thread. I apologize for bothering you with such a simple question, but I have been struggling for an hour and still can't figure it out. Thank you again for your help.
Here is my code:

Code: Select all

#Requires AutoHotkey v2.1-alpha.10
#SingleInstance Force

; Create a new thread and save its thread ID
dll := NewThread("
(
    Loop {
        ToolTip 'Thread running'
        Sleep 1000
    }
)")

Sleep 2000

; Attempt to terminate the newly created thread
Thread("Terminate", false, dll)

Sleep 2000

MsgBox "End"
image.png
image.png (12.23 KiB) Viewed 5124 times

[Mod edit: Again, fixed the codebox tags. They go around the code, not both tags before the code.]
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: thqby's AutoHotkey_H v2.0 & v2.1

13 May 2024, 22:39

@changxiaotiao — Twice now we have fixed your codebox tags. The code goes in between the pair of tags, not after them. Please place the tags correctly when you post code. You can also use [code][/code] tags, which default to AHK syntax highlighting. They also go around the code, and there is an icon for their use above the reply area.
changxiaotiao
Posts: 6
Joined: 12 May 2024, 19:52
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

13 May 2024, 23:16

boiler wrote:
13 May 2024, 22:39
@changxiaotiao — Twice now we have fixed your codebox tags. The code goes in between the pair of tags, not after them. Please place the tags correctly when you post code. You can also use [code][/code] tags, which default to AHK syntax highlighting. They also go around the code, and there is an icon for their use above the reply area.
I'm sorry for the mistakes I made in formatting the code. Thank you so much for fixing them.
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

14 May 2024, 03:08

idk if you can use normal newthread object but i found this

Code: Select all


myvar := 50

Script := "
(
	MsgBox Worker(A_MainThreadID)['myvar']
)"
Worker(Script)
MsgBox "end"

source := https://github.com/thqby/AutoHotkey_H/issues/74
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
changxiaotiao
Posts: 6
Joined: 12 May 2024, 19:52
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 02:02

@xMaxrayx

Sorry, I don't know how to reply to your private message. I can now use multithreading, but I'm having an issue with the terminate function. For now, I've used ahkPause as a substitute, and the script runs fine.
User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 03:18

changxiaotiao wrote: Sorry, I don't know how to reply to your private message.
While still in the “new member” status, you will not be able to send private messages.
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 10:10

changxiaotiao wrote:
15 May 2024, 02:02
@xMaxrayx

Sorry, I don't know how to reply to your private message. I can now use multithreading, but I'm having an issue with the terminate function. For now, I've used ahkPause as a substitute, and the script runs fine.
I see many thanks, im not good with AHK-H but I found "ExitApp" method with class worker

Code: Select all

class Worker {
	/**
	 * Enumerates ahk threads.
	 * @return An enumerator which will return items contained threadid and workerobj.








	 * Terminate the thread asynchronously.
	 */
	ExitApp() => void


-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 10:31

Ok I found something weird if the thread closed it will close the Msgbox but not with tooltip. maybe it's a bug

Code: Select all

script:= ("
(   
    MsgBox 'hello from another thread'
    i := 1
    while i != 0 {
        ToolTip '<3'
        
    }
)")

; Worker(0,script,"sss")
L := worker(script,,"ss" )
Sleep(500)
L.ExitApp() ; it close the 'hello from another thread' but not the tool top





MsgBox "hello from main script"
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 10:50

Ok i found it close the msgbox but not with loop and soundplay

Code: Select all

script:= ("
(   
    MsgBox "lol"
    SoundPlay("C:\Users\Max_Laptop\Documents\Audacity\waiting for a window.wav" , 1)
    OnExit ExitFunc
    ExitFunc(*){
        MsgBox 'good bey'
    }
    
)")




; Worker(0,script,"sss")
L := worker(script,,"pizaa" )
Sleep(500)
L.ExitApp() ;it closes msgbox








MsgBox "hello from main script"


-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 11:14

can you tell me how did use ahkpause?

according to this doc
https://hotkeyit.github.io/v1/docs/commands/ahkPause.htm

my code (i added 64dll)

Code: Select all

dllpath:=A_AhkDir "\AutoHotkey64.dll"
DllCall("LoadLibrary","Str",dllpath) ; Load the AutoHotkey module.
ThreadID:=DllCall(dllpath "\NewThread","Str","
(
Persistent
Loop
  ToolTip A_TickCount
)","Str","","Str","","CDecl") ; start a new thread, just the function.
Sleep 1000
DllCall(dllpath "\ahkPause","Str","On","Uint",ThreadID,"CDecl") ; Pause thread.
MsgBox "End"

dll:=NewThread("
(
Persistent
Loop
	ToolTip A_TickCount
)",,dllpath)
Sleep 1000
dll.ahkPause("On")
MsgBox "End"

Code: Select all

Error: This value of type "Integer" has no method named "ahkPause".

	073: dll := NewThread("Persistent
Loop
	ToolTip A_TickCount",,dllpath)
	079: Sleep(1000)
▶	080: dll.ahkPause("On")
	081: MsgBox("End")
	082: Exit
I cant use ahkpause with DLL object
image.png
image.png (29.18 KiB) Viewed 4858 times
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
changxiaotiao
Posts: 6
Joined: 12 May 2024, 19:52
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 11:27

@xMaxrayx
Yes, I also found that ToolTip cannot be closed this way. Can you try this code ?

Code: Select all

dll["Thread"]("Terminate", true)
I saw the author on GitHub mentioned that it's possible to forcibly break the loop, but I tested it and it didn't work. MsgBox also couldn't break the loop.

It might be a version issue. The usage of ahkPause has changed. You can try

Code: Select all

ahkPause(1, dll).
You can see the explanation at this link.https://github.com/thqby/AutoHotkey_H
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 11:55

I found this error maybe interesting
code :

Code: Select all

string1:="
(
SoundPlay("C:\Users\Max_Laptop\Documents\Audacity\waiting for a window.wav",1)
)"

dll1:=  Worker(string1 )
Sleep(1)
dll1.ExitApp()
Sleep(500)
dll1.Reload() 
dll1:=Worker(string1)


error :

Code: Select all

Error: (0x80010012) The callee (server [not server application]) is not available and disappeared; all connections are invalid. The call did not execute.

	081: dll1.ExitApp()
	082: Sleep(500)
▶	083: dll1.Reload()
	084: dll1 := Worker(string1)
	085: Exit



soundplay still running after dll1.ExitApp() but dll1.Reload() can't be work ,

so what's the thread who do the soundplay? is the thread was real?

i did this code and I got this

Code: Select all

MsgBox A_MainThreadID "`n" dll1.ThreadID
image.png
image.png (92.57 KiB) Viewed 4832 times
I tried relauding dll1 without using exitapp() and turn out the audio running twice.

Code: Select all

string1:="
(
SoundPlay("C:\Users\Max_Laptop\Documents\Audacity\waiting for a window.wav",1)
)"

dll1:=  Worker(string1, ,"pizaa") 
Sleep(1)
; dll1.ExitApp()
Sleep(500)

MsgBox A_MainThreadID "`n" dll1.ThreadID


dll1.Reload() 
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
User avatar
xMaxrayx
Posts: 352
Joined: 06 Dec 2022, 02:56
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 13:02

changxiaotiao wrote:
15 May 2024, 11:27
@xMaxrayx
Yes, I also found that ToolTip cannot be closed this way. Can you try this code ?

Code: Select all

dll["Thread"]("Terminate", true)
I saw the author on GitHub mentioned that it's possible to forcibly break the loop, but I tested it and it didn't work. MsgBox also couldn't break the loop.

It might be a version issue. The usage of ahkPause has changed. You can try

Code: Select all

ahkPause(1, dll).
You can see the explanation at this link.https://github.com/thqby/AutoHotkey_H
yeah sadly i tried it again and didn't work the weird part tooltip can't be work with "worker" class unlike with "NewThread" class


thanks I managed to pause the thread with ahkPause but it doesn't work for soundplay , it plays the whole music length then pause, it's like ahkPause pause between two process but not while the current process still running and not finished.


Code: Select all

f::{
    Critical
    ahkPause(1, dll)
    ToolTip "yes"
}
;/####

/*
dll := Worker(script)
script := ("
(
    Loop {
        ; SoundPlay("C:\Users\Max_Laptop\Documents\Audacity\waiting for a window.wav",0)
        ToolTip 'Thread running'
        Sleep(1000)
    }
)")
*/



dll:= NewThread("
(
    Loop {
        SoundPlay("C:\Users\Max_Laptop\Documents\Audacity\waiting for a window.wav")
        ; ToolTip 'Thread running'
        Sleep 200
    }
)")



; dll["thread"]("Terminate", true)
; Critical
; Thread("Interrupt", dll)
; ahkPause(1, dll.ThreadID)
; dll.ExitApp()


Sleep 500
; ahkPause(1, dll)
MsgBox 'end'
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
changxiaotiao
Posts: 6
Joined: 12 May 2024, 19:52
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

15 May 2024, 23:07

I have tested and come to the same conclusions as you:

1: pause will only take effect after the current thread finishes.
2: dll.Reload() can work twice.
3: If ExitApp is called first, followed by Reload, an error will occur.
However, I found that ExitApp can stop the sound playback

Code: Select all

dll:=Worker("
(   
	loop{
		soundplay("c:\Program Files\AutoHotkey\Media\test2.wav",0)
		sleep 5000
		;msgbox "runing"

	}
	
)")

Sleep 10000
dll.ExitApp()
;dll.Reload()
Persistent
The external Sleep determines the entire thread's duration, while the internal Sleep determines how long the current loop runs, but it will never exceed the total thread duration.
and the second parameter of SoundPlay must be 0
User avatar
thqby
Posts: 560
Joined: 16 Apr 2021, 11:18
Contact:

Re: thqby's AutoHotkey_H v2.0 & v2.1

16 May 2024, 06:04

worker_obj.ExitApp() cannot be used to exit a thread in a loop.

Forcing thread exit will result in a large amount of resources not being released properly.

worker_obj['Thread']('Terminate', true) can interrupt the loop, but it may also prevent the release of some resources.

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 11 guests