How do I reuse a thread? Topic is solved

Ask for help, how to use AHK_H, etc.
Splongus
Posts: 9
Joined: 27 Jun 2020, 23:15

How do I reuse a thread?

21 Sep 2020, 10:48

[AHK_H v1.1] Hi. I've looked through all of the docs and many forum posts involving HotKeyIt's responses, but man it's hard to grasp how to get a proper thread running. I'm using the AutoHotkey.dll included in my installation (so not currently intending to copy the .dll and rename it or whatever that's all about)

I need to start a new thread from my main script and routinely run code in it from one specific main script string. I'm currently using `AhkThread("msgbox,hello world")` to both create and execute the thread in one line, on a ~1s loop (main script has to invoke it each time). Of course I get a memory leak and it has destroy/init the thread every time, both of which aren't preferable.

I have great respect for the work involved with the project and it's likely a me-problem... the AHK_H multithread docs are uninterpretable for me. The descriptions for each item, code examples, purpose, explanations of differences between similar looking items... with my basic understanding of AHK_L v1.1 vs v2 and how the docs are all written for v2 but mostly apply to v1.1, the multithreading docs appear to be written in what I interpret to be some kind of alien language. I must decode it!

Ramble aside, thanks for any help :)

Here's an oddly simple and pointless version of what I currently have, if it helps:

Code: Select all

#Persistent
MySub:
	AhkThread("SoundPlay,*-1") ; bad method
	Random,randomvar,1000,2000
	settimer,MySub,% randomvar
return
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How do I reuse a thread?

21 Sep 2020, 11:58

Are you using the latest version, it works fine for me.
Splongus
Posts: 9
Joined: 27 Jun 2020, 23:15

Re: How do I reuse a thread?

21 Sep 2020, 12:13

@HotKeyIt
v1.1.32.00 (EDIT sorry used to say 1.1.31.00, it's 32), \x64w_MT\AutoHotkey.exe, from: https://github.com/HotKeyIt/ahkdll-v1-release/archive/master.zip
private bytes climbing about 1-2MB/sec

listlines shows this every time AhkThread runs:

Code: Select all

---- AhkThread.ahk
017: if !base  
021: obj := {(""):lib:=MemoryLoadLibrary(dll=""?&ahkdll:dll="FC2328B39C194A4788051A3B01B1E7D5"?&ahkmini:dll),base:base}
022: For k,v in functions
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
023: obj[k]:=DynaCall(MemoryGetProcAddress(lib,A_Index>2?k:SubStr(k,2)),v)  
024: if !(s+0!="" || s=0)  
025: obj.hThread:=obj[IsFile?"ahkdll":"ahktextdll"](s,"",p)   (0.05)
026: ahkthread_free(true)[obj]:=1  
003: if !objects  
005: if (obj="")  
007: if !IsObject(obj)  
008: Return,objects
027: Return,obj
i'm looking to avoid the memory leak and to reduce time/processing each time the thread runs code from the main script's string, preferably by not creating/freeing a new thread each time, right?
Splongus
Posts: 9
Joined: 27 Jun 2020, 23:15

Re: How do I reuse a thread?

21 Sep 2020, 12:45

accidentally misspelled version, it's v1.1.32.00, not 1.1.31.00
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: How do I reuse a thread?  Topic is solved

22 Sep 2020, 18:57

Code: Select all

#Persistent
ahkdll:=AhkThread(0)
MySub:
	ahkdll.ahktextdll("SoundPlay,*-1")
	Random,randomvar,1000,2000
	settimer,MySub,% randomvar
return
Splongus
Posts: 9
Joined: 27 Jun 2020, 23:15

Re: How do I reuse a thread?

23 Sep 2020, 10:55

Thanks for the solution! I definitely wouldn't have figured it out looking at the docs :? No memory leak and the main script only spends 8-16ms initializing the thread which really isn't bad, + both run simultaneously (or at least I think it's truly simultaneous...)

Return to “Ask for Help”

Who is online

Users browsing this forum: No registered users and 24 guests