Thread


Multithreading is based on AutoHotkey.dll (only AutoHotkey.dll 1.0.48.3 is supported at the moment).
Thread function will help you to use multithreading.

Thread([file_or_text,parameters,dllpath])

Parameters

Parameter

Info

file_or_text

Thread can execute files as well as text. So this parameter can be a file path or text.

Otherwise this parameter can be the thread handle, see parameter for available actions.

parameters

You can pass on parameters to your thread as usual.

When file_or_text is a thread handle, this parameter can be following value
- Priority or P or 1, changes priority of thread. dllpath must contain new priority
      -15      Idle
        -2      Lowest
        -1      Below normal
         0      Normal
         1      Above normal
         2      Highest
       15      Critical
- Kill or K or 0, terminates a thread
- Suspend or S or 2, pause a thread
- Resume or R or 3, resume paused thread
- Check or C or 4 wil check if a thread exists.

dllpath

Path of AutoHotkey.dll.
You can leave it empty if AutoHotkey.dll exist in scripts directory, AutoHotkey.exe path or Lib folder.

- When killing or unloading thread, this parameter should be the library pointer so calling Thread(handle,0,lib) will kill thread and free library.

Return Value

Thread returns a thread id that can be used to terminate the thread.
ErrorLevel will contain the pointer to the library.
See other return values in remarks.

Remarks

A thread can also exit itself (like ExitApp in AutoHotkey) using DllCall("ExitThread","Uint",0)
When checking for Thread, return value will be 1 if it exist 0 if not and 259 if it is still active. ErrorLevel will contain exit code of the thread if it still exist.

Read about Multithreading Consideration and Terminate Thread on MSDN

Related

None.

Example

#NoEnv

var(1="",2="",3=""){ ;declare dictionaryfunction
}

OnMessage(0x9999,"ExitThread")
Gui, +LastFound
hwnd:=WinExist()

DDB()
Script=
(LTrim`t
#NoEnv
#NoTrayIcon
DetectHiddenWindows,On
var(1="",2="",3=""){ ;you need to declare the same dictionaryfunction again to use it!!!
}
DDB()
SetBatchLines,3
Loop
var(1, A_TickCount)
ExitApp
e::SendMessage,0x9999,A_Thread,,,ahk_id %hwnd% ;A_Thread and #Persistent are added to top of script when calling Thread()
#Include %A_ScriptDir%\DDB.ahk
)
threadID:=Thread(script) ;start new thread
lib:=ErrorLevel
Thread(threadID,1,-15) ;change threads priority
SetTimer,ToggleSuspend,1000
SetBatchLines,10
count=1
Loop
ToolTip % "A_TickCount is set in a separate thread.`nMain process displays this ToolTip + pauses and resumes the thread each second.`nPress Esc to exit`nPress e to exit/kill thread (you can start a new one by pressing n)`nThreadcount: " count "`n`n" var(1)

~Escape::
Exit:
Thread(threadID,0) ;kill thread (not really required)
DDB()
ExitApp

n::
Thread(threadID,0,lib)
threadID:=Thread(script) ;start new thread
Thread(threadID,1,-15) ;change threads priority
count++
Return
ToggleSuspend:
If (suspended:=!suspended)
Thread(threadID,2) ;suspend thread
else
Thread(threadID,3) ;resume thread
Return

ExitThread(wParam){
    global
    Thread(wParam,0,lib)
}

#include %A_ScriptDir%\DDB.ahk
#include %A_ScriptDir%\Thread.ahk